CMS Development

redbranchmedia
Contributor | Elite Partner
Contributor | Elite Partner

Sort order of blog topics by order added to post

SOLVE

Hello! 

I'm not sure if this is possible, but that's why I'm checking here. I've seen a number of posts on sorting blog topics alphabetically, but I'd like to output blog topics on the order added to the post. 

 

Example, if I add Security, Intelligence, Mapping, I want them to appear in that order. However, by default, they seem to appear in .. I don't know, the order the tags were created maybe? I can alphabetize them like here but that's not really what I'm looking for. I'm just using the default loop for the time being. 

 

 

{% if content.topic_list %}
{% for topic in content.topic_list %}
{{ topic.name }}{#% if not loop.last %},{% endif %#}
{% endfor %}
{% endif %}

 

 

 

 

Screen Shot 2021-11-09 at 14.30.56.png

0 Upvotes
1 Accepted solution
Anton
Solution
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

Sort order of blog topics by order added to post

SOLVE

Hi @redbranchmedia

it will work this code.

I've created an extra module but you can do it directly in the listing HTML

 

 

{% set blog_tags = blog_tags('module.blog_field', 250) %}

{% set sorted_tags = blog_tags|sort(False, False, 'name') %}

{% set tag_posts = blog_recent_tag_posts(blog_tags, sorted_tags, 100)|sort(False, False, 'name') %}


{% for post in tag_posts %}
    <div class="post-title">{{ post.name }}</div> {# replace this with your single post layout #}
{% endfor %}

 

 

edit: 

If you decide to create a custom module simply add a "blog" function. By default it will be called "blog_field". If you give it a custom name replace it in the first line of code.

 

 

 

best, 

Anton

Anton Bujanowski Signature

View solution in original post

6 Replies 6
Anton
Solution
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

Sort order of blog topics by order added to post

SOLVE

Hi @redbranchmedia

it will work this code.

I've created an extra module but you can do it directly in the listing HTML

 

 

{% set blog_tags = blog_tags('module.blog_field', 250) %}

{% set sorted_tags = blog_tags|sort(False, False, 'name') %}

{% set tag_posts = blog_recent_tag_posts(blog_tags, sorted_tags, 100)|sort(False, False, 'name') %}


{% for post in tag_posts %}
    <div class="post-title">{{ post.name }}</div> {# replace this with your single post layout #}
{% endfor %}

 

 

edit: 

If you decide to create a custom module simply add a "blog" function. By default it will be called "blog_field". If you give it a custom name replace it in the first line of code.

 

 

 

best, 

Anton

Anton Bujanowski Signature
Oezcan
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Sort order of blog topics by order added to post

SOLVE

Hello @redbranchmedia ,

Unfortunately, I don't know the default behavior. But Google a little and found something like this:

https://knowledge.hubspot.com/knowledge-base/organize-your-knowledge-base-categories


https://community.hubspot.com/t5/Releases-and-Updates/Now-Live-Knowledge-Base-Re-order-Categories-an... 

https://kinsta.com/knowledgebase/how-to-reorder-posts-in-wordpress/ 

 

and this:

function change_posts_order( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', ' change_posts_order ' );

 

I hope this is what you are looking for and it will help you.

 

Otherwise Senpai @Anton might be able to help 😁

 

Best regards

Özcan

Oezcan Eser Signature
Oezcan
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Sort order of blog topics by order added to post

SOLVE

Hey @natsumimori, and hi @redbranchmedia  ,

it makes me very proud and glad that my thoughts are asked.

Thanks very much :).

I have to admit that I haven't come into contact with it yet.
But I do my best and I came across it in my research and I think that would be the solution.

I hope that I could help with that.

Best regards
Özcan

Oezcan Eser Signature
redbranchmedia
Contributor | Elite Partner
Contributor | Elite Partner

Sort order of blog topics by order added to post

SOLVE

Thanks again for your help. Unfortunately, that doesn't look like it'll work for what I need. That function allows me to return a specific tag or array of tags from a blog. I want to return the blog tags from the post itself in the order in which they're added in the back-end. Do you know what the default behavior is? I can't seem to find info on that either. It seems to maybe be based on when the tags were initially created in the blog itself?

0 Upvotes
redbranchmedia
Contributor | Elite Partner
Contributor | Elite Partner

Sort order of blog topics by order added to post

SOLVE

Thank you! I will test that out and see if that does the trick.

 

0 Upvotes
natsumimori
Community Manager
Community Manager

Sort order of blog topics by order added to post

SOLVE

Thank you for your post @redbranchmedia !

 

Hi @Oezcan , would you mind sharing your thought here?