Support » Developing with WordPress » Change category default order – comments descending

  • Hi

    I want to change wordpress posts default order. Default order is date descending. I want to change this to comments descending. I want to make this change with functions.php so that my changes dont go after wordpress updates. I am unable to get the correct code. Please help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello @simranjeetz

    According to your requirement, please try below code in functions.php file.

    function set_post_order_in_admin( $wp_query ) {
    	if ( is_admin() )
    	{
    		$wp_query->set( 'orderby', 'content' );
    		$wp_query->set( 'order', 'DESC' );
    	}
    }
    add_filter('pre_get_posts', 'set_post_order_in_admin' );

    This code will sort your posts by your content in DESC order.

    • This reply was modified 3 months, 2 weeks ago by  harshbarach. Reason: more details
    • This reply was modified 3 months, 2 weeks ago by  harshbarach. Reason: more details

    thanks a lot.
    I have several posts. When I use this code and go to a category page, my posts are not shown in comments number descending order. This is therefore not working.

    Moderator bcworkz

    (@bcworkz)

    $wp_query->set( 'orderby', 'comment_count' );

    Remove the is_admin() conditional if you want this to apply to both front and backend queries. The conditional you want is at least if ( $wp_query->is_main_query() ) otherwise it will be applied to things like menu item queries which of course have no comments.

    Consider adding a secondary orderby for when the comment counts are equal. Perhaps ‘comment_count date’.

    Depending on your theme, functions.php may not be safe still. Your code can be overwritten if the theme is updated. Does not apply if the theme is yours, such as your own child theme.

    I used this

    
    function set_post_order_in_admin( $wp_query ) {
    	if ( $wp_query->is_main_query() )
    	{
    		$wp_query->set( 'orderby', 'comment_count' );
    		$wp_query->set( 'order', 'DESC' );
    	}
    }
    add_filter('pre_get_posts', 'set_post_order_in_admin' );
    

    Code didnt work. I got latest posts on top. Not posts with max comments. Can you also suggest how to add second conditional. Also I want no changes in admin. Only front-end in category list. Thanks for this

    • This reply was modified 2 months, 4 weeks ago by  simranjeetz.
    • This reply was modified 2 months, 4 weeks ago by  simranjeetz.

    used this and it worked.

    
    function my_pre_get_posts( $query ) {    
            $query->set( 'orderby', 'comment_count' );
        return $query;
    }
    

    Please check if this is correct. Also please tell me how to add second conditional. Problem is that this also changed my admin area post order which i dont want. How to correct this?

    
    function my_pre_get_posts( $query ) {
        if ( is_category() )
            $query->set( 'orderby', 'comment_count' );
        return $query;
    }
    

    Now I need second conditional only

    Moderator bcworkz

    (@bcworkz)

    Do you mean a second orderby term to use when counts are equal? (A “conditional” would be criteria within the if () statement) Just concatenate terms together in the same set() statement.
    $query->set( 'orderby', 'comment_count date' );

    That is for post date, not comment date. The terms you use need to be valid WP_Query orderby terms. A different filter and approach is required for table columns that do not have valid WP_Query orderby terms.

    Sorry about misleading you with is_main_query(). I made a general assumption which does not apply to your situation.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.