0

I'm using the following filter in order to increase the amount of posts shown on category templates from 10 to 30 (I want to keep 10 for the rest of the site)

// Modify number of results shown
function modify_query_amount_shown($query){
    if ($query->is_category) {
        $query->set('posts_per_page', 30);
        $query->set('orderby', 'menu_order');
    }
    return $query;
}
add_filter('pre_get_posts', 'modify_query_amount_shown');

However, when I refresh my page several times, I notice the order of my posts changes each time. I can confirm that it has something to do with this code, because as soon as I comment it out, it returns to normal.

1 Answer 1

2

pre_get_posts is not a filter hook, it is an action. Also you should not set orderby to menu_order for posts as they are not hierarchical. Set orderby to date instead.

function modify_query_amount_shown( $query ) {
    if ( $query->is_category ) {
        $query->set( 'posts_per_page', 30 );
        $query->set( 'orderby', 'date' );
    }
}
add_action( 'pre_get_posts', 'modify_query_amount_shown' );

Now the order of posts displayed will be consistent, regardless of the number of refreshes.

1
  • Thank you! How silly...by the way I was able to remove the orderby completely and it still works as intended. Commented Sep 13, 2017 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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