1

I am trying to get this template to work.

1
  • You shouldn't create your own custom WP_Query in category template. You should use global one. And if you need to modify it, you should use pre_get_posts action to do it. Commented Jan 28, 2019 at 12:33

1 Answer 1

0

You should never create your own custom WP_Query queries on category templates (or any other archive template) and use them as main loop.

Why? Because it will break pagination. WordPress will still create all pagination links for such archives based on default query.

So if you want to modify the query in such way, that is should get all posts ordered by date, then you should use pre_get_posts action (add this to functions.php file):

function show_all_posts_on_category_archives( $query ) {
    if ( ! is_admin() && is_category() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', -1 );
        $query->set( 'orderby', 'date' );  // you shouldn't need this one - date is default value
    }
}
add_action( 'pre_get_posts', 'show_all_posts_on_category_archives' );

And after using that code you can use typical loop in your template file:

while ( have_posts() ) : the_post();
    ...
endwhile;
2
  • Yes, after adding it to functions.php you should see all posts on category archive. Commented Jan 28, 2019 at 12:44
  • It's because the action above is checking for the category archive to modify only queries for this template. If you want to modify other query, you'll have to target other page with the condition inside your action. Commented Jan 28, 2019 at 13:01

Your Answer

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