0

I am using WP-PageNavi pagination and because of certain reasons I have "Blog pages show at most" set to 100 and I can't reduce it.

I have a total of 30 posts in a category. I want to display 10 posts on each page but it doesn't display pagination; it displays only if a category has more then 100 posts. Code is below:

$cat_id = get_query_var( 'cat' ); 
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$found_posts = array(
    'posts_per_page'      => 10,
    'cat'                 => $cat_id,
    'meta_key'            => "custom_key",  
    'meta_value'          => '',
    'meta_compare'        => '!=',
    'ignore_sticky_posts' => 1,
    'orderby'             => 'meta_value_num', // or 'meta_value_num'
    'order'               => "DESC",
    'paged'               => $paged
);
$loop = new WP_Query( $found_posts );
while( $loop->have_posts() ): $loop->the_post();
    global $post;

    // Loop Here

endwhile; 
wp_reset_query();
if ( function_exists( 'wp_pagenavi' ) ) { 
    wp_pagenavi(); 
}
2

Do not run custom queries in place of the main query on the home page and any type of archive page. This will always cause an issue. If you need to alter the main query, use pre_get_posts to do so.

To solve this issue, you need remove your code from your category page and go back to the default loop. You should only have the following in your category page

if ( have_posts() ) {
    while ( have_posts() ) {
    the_post();
        // YOUR MARKUP AND TEMPLATE TAGS
     }
}

This will led that you will see all category posts on the category post ordered by post date. As I said, you now need to use pre_get_posts to alter the main query on category pages to set your custom ordering and pagination

You need to add the following in functions.php

add_action( 'pre_get_posts', function ( $q )
{
    if (    !is_admin() // Only target the front end queries
         && $q->is_main_query() // Targets the main query only
         && $q->is_category() // Only target category pages
    ) {
        $q->set( 'posts_per_page', 10 );
        $q->set( 'meta_key', 'custom_key' );
        $q->set( 'meta_value', '' );
        $q->set( 'meta_compare', '!=' );
        $q->set( 'orderby', 'meta_value_num' );
        $q->set( 'ignore_sticky_posts', 1 );
    }
});

You should no see 10 posts per page ordered by meta_value_num from the custom field custom_key sorted highest to lowest on your category pages

  • Pieter, It works great but as per your previous answer wordpress.stackexchange.com/questions/194628/…. I have added rank function before loop It works perfect in main page but rank is not showing up in category page. – Hussain Ansari Jul 20 '15 at 18:18
  • Just do $rank = get_post_rank( $wp_query ); – Pieter Goosen Jul 20 '15 at 18:24
  • I have updated the post rank function. Inside the main query you can now just do $rank = get_post_rank();. For custom queries you still need to do $rank = get_post_rank( $q );. Check out the updated version here. PS!!! Don't forget to accept my answer if it worked ;-) – Pieter Goosen Jul 20 '15 at 18:39
  • Search isn't working pagination. – Hussain Ansari Jul 21 '15 at 17:04
  • Please rephrase. If I read this correctly, then the pagination on the search page is not working, if so, this has nothing to do with this issue – Pieter Goosen Jul 21 '15 at 17:09
0

In addition to the "selected answer", this might be needed:

$q->set( 'post_type', get_post_types() );

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.