In wordpress, right now only on my main blog page, I managed to sort all posts with:

<?php if ( is_active_sidebar( 'blog-category' ) ) : ?>
                    <?php dynamic_sidebar( 'blog-category' ); ?>
                <?php endif; ?>
              <div class="blog_list_content">
           global $wp_query;
             $args =  array(
                'meta_key' => 'publish_date',
                'orderby' => 'meta_value',
                'order' => 'DESC'
            $args = array_merge( $wp_query->query, $args );
            query_posts( $args );
           if (have_posts()) :
               while (have_posts()) : the_post();
                    get_template_part( 'template-parts/content', get_post_format() );

When I click a blog category, the sorting doesn't work. Only on the main blog page. What do I need to do in order to sort the posts on other categories in the same way?


2 Answers 2


First of all, you should not use custom query just to change the order of posts. This may cause problems with pagination and definitely is not optimal.

So first things first. Remove that part of your code:

        global $wp_query;
        $args =  array(
            'meta_key' => 'publish_date',
            'orderby' => 'meta_value',
            'order' => 'DESC'
        $args = array_merge( $wp_query->query, $args );
        query_posts( $args );

All it does is changing few params and calling the query again. But there is an action, that allows you to add your custom parameters before running the query: pre_get_posts. And you can use it to modify order on category archives too:

function my_set_custom_order( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {  // modify only main query on front-end
        if ( is_home() ) {
            $query->set( 'meta_key', 'publish_date' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'DESC' );
        if ( is_category( 'cats' ) ) {
            $query->set( 'meta_key', 'cat_name' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );                
        // ...
add_action( 'pre_get_posts', 'my_set_custom_order' );

This will sort your homepage DESC by publish_date and your cats category ASC by cat_name.

You can add whatever you want/need in there and you can use Conditional Tags to modify queries only for some requests.


You most likely updated your index.php right? In the case of category pages you can use category.php (or archive.php but category.php is more specific).

The Codex has a good example that you could adapt fairly easily: https://codex.wordpress.org/Category_Templates#Different_Text_on_Some_Category_Pages

<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages, 
I could be left blank</p>
<?php endif; ?>

Alternatively, you could get VERY specific, and customize by category slug, having a unique template for every category you create. So if you had three categories called:

  • Cats
  • Dogs
  • Hamsters

You would have three templates called:

  • category-cats.php
  • category-dogs.php
  • category-hampsters.php

In order to have custom sorting on each of those templates, you'd customize the wp_query args with the "orderby" argument. See here: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

I hope that clears it up a bit. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.