Support » Developing with WordPress » Sort posts on Custom Taxonomy page

  • Hi,

    I’m working with a site for a magazine and they publish all their articles from their issues on the site, and we’re using a Custom Taxonomy to categorize the articles into the different issues. I’ve created a specific template to show the Custom Taxonomy (‘taxonomy.php’).

    The main feed of articles on the site is sorted from the latest to the oldest (the latest article appears first on the site). My problem now is that I want to sort the articles on the issue-pages (that are using taxonomy.php) in reverse order compared to the main feed (from the oldest to the latest).

    Can anybody help me with that?

    For the moment my code looks like this, but this results in the issue-pages showing all of the posts of the site (although ordered in the way I want):

    $cat_posts = new WP_Query($query_string."&orderby=date&order=ASC");
    if ( $cat_posts->have_posts() ) {
        while ( $cat_posts->have_posts() ) : $cat_posts->the_post();

    Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz


    What is the value of $query_string? That is what determines which posts are returned. What is it you want returned that’s not happening? The posts_per_page quantity? Anything else?

    FWIW, I’d urge you to adopt the newer array style of arguments. There are some very useful argument structures that can only be accomplished with the array style. The string style is still functional mainly for backward compatibility.

    Well, I got this solution from a website that I can’t find right now, so I don’t really know how it all works.

    So, the posts on the page is sorted into different issues and what I try to achieve is to display the posts from just one issue and sort them from the oldest to the newest (on the contrary to the “main feed” that is sorted from the newest to the oldest). I can get the site to display the right posts, but not to sort them in the right way.

    The solution above displays all the posts from all the issues when I click on an issue, although they are sorted in the right way. Note that I’m not using that solution right now.

    Here is the page with all the issues:

    And here is a page with the posts from just one issue (but sorted in the wrong way):

    Moderator bcworkz


    FWIW, when you use a taxonomy.php template, it will be used for all taxonomy requests except when there is a more specific template like category.php. Perhaps for your site that arrangement is fine. If you did need to have your template apply to only your specific taxonomy, name the template taxonomy-{$tax-slug}.php where you replace {$tax-slug} with your actual taxonomy name or slug.

    You should not need to create a new query for this. The original main issue query should work for your needs. By default the posts are listed in the wrong order, but that can be corrected easily. Creating a new query to replace the original is wasteful since you force WP to get all the proper issues from the DB, only to be ignored and asked to get the issues all over again in a different order.

    Maybe that makes little difference now. On very large DBs and/or heavily trafficked sites it will make a difference. To do this properly, you need a template that just runs the standard Loop, no queries for content are needed. Probably like the archive.php template that is part of your theme. If your taxonomy.php has some other useful content you want to retain, you would need to remove the custom query and convert your loop to the standard WP Loop. Things like $cat_posts->have_posts() become just have_posts(), etc.

    Another advantage of correcting the main query is pagination, if needed, will work without any problems.

    To correct the order of posts, we hook the “pre_get_posts” action. We confirm the query is indeed one that we want to alter. If so, we simply set the ‘order’ query var to be ASC. If there were anything else to correct, it can be done here as well. I believe all the defaults will meet your needs, so setting that one query var should do it. The following code would go on your theme’s (preferably child theme's) functions.php.

    add_action('pre_get_posts', 'fix_nummer_order');
    function fix_nummer_order( $query ) {
       if ( $query->is_main_query() && '' != $query->get('nummer')) {
          $query->set('order', 'ASC');

    Untested. Let me know if you have any difficulty. I’m assuming your taxonomy name or slug is “nummer”, if not adjust the code accordingly.

    Hi @bcworkz,

    This is a very late answer but your solution seems to work and I want to say thousand thanks for that! And thanks for your education as well! 😉

    / Isak

    Moderator bcworkz


    You’re welcome. And thanks for coming back, no matter how much later. I’m glad you found my long post informative.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sort posts on Custom Taxonomy page’ is closed to new replies.