0

On every archive page I'm either using WP-queries and no paging, or paging without a WP-query and a regular old "if(have_posts)"

Now on one archive page I want to display more than 8 posts (8 is in settings for all my normal archive pages) So I thought about using a WP-query, but when I use one and set it to display 10 posts, the paging also breaks.

Any suggestions?

2
  • pre_get_posts Commented Sep 15, 2015 at 13:53
  • 1
    There is no reason to build archive pages using secondary queries/loops. When the template file is reached, the query for standard archive has been performed, if you don't use it and make a new query, you are wasting server resources. Instead, you should use pre_get_posts action hook to alter the arguments of the main query; in this way, when the template file is reached, you have exactly the results you want with no unnecesary queries. Addtionally, you don't need to worry about pagination in the custom loop, it is already hadle by WordPress.
    – cybmeta
    Commented Sep 15, 2015 at 14:12

1 Answer 1

1

pre_get_posts did the trick (thanks Pieter and cybmeta!

Here's the solution:

function hwl_home_pagesize( $query ) {
if ( is_post_type_archive( 'motivation' ) ) {
    $query->set( 'posts_per_page', 9 );
    return;
}
if ( is_post_type_archive( 'blog' ) ) {
    $query->set( 'posts_per_page', 14 );
    return;
}
}
add_action( 'pre_get_posts', 'hwl_home_pagesize', 1 );

That's it!

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.