• Hi,
    I’m new to WordPress.
    I’am having problems with a pagination I made.
    Everything works fine if I show all the posts with this query:

    $args = array('posts_per_page' => 7, 'paged' => $current_page, 'post_type' => 'post', 'suppress_filters' => false);
    $postslist = get_posts( $args );
    global $wp_query;
    $totalpages = $wp_query->max_num_pages;

    In this case, the value of $totalpages = 94 which is correct.

    However, the visitors should also be able to select that they only want to see the posts which belong to a certain category. In that case, I add category to the arguments array like this:
    $args = array('posts_per_page' => 7, 'paged' => $current_page,'category' => $category_id, 'post_type' => 'post', 'suppress_filters' => false);
    The posts of the correct category are shown correct on my page, however
    `global $wp_query;
    $totalpages = $wp_query->max_num_pages;
    stills gives me the result 94 pages, which is not correct because 94 pages is the correct amount of pages for all categories.
    Can someone explain me how I can find the total number of pages when I query the posts of 1 category?

    Thanks in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • try and work with WP_Query() instead of get_posts();

    example:

    $args = array('posts_per_page' => 7, 'paged' => $current_page,'category' => $category_id, 'post_type' => 'post', 'suppress_filters' => false);
    $postslist = new WP_Query( $args );
    $totalpages = $postslist->max_num_pages;

    you will also need to adjust the way you output the posts.

    review https://codex.www.ads-software.com/Class_Reference/WP_Query

    Thread Starter synthi100

    (@synthi100)

    Thanks for you reply.
    I tested it like this but the result is still 94.

    both

    $args = array('posts_per_page' => 7, 'paged' => $current_page,'category' => $category_id, 'post_type' => 'post', 'suppress_filters' => false);
    $postslist = new WP_Query( $args );
    die ($postslist->max_num_pages);

    (with category in the args array)
    and

    $args = array('posts_per_page' => 7, 'paged' => $current_page, 'post_type' => 'post', 'suppress_filters' => false);
    $postslist = new WP_Query( $args );
    die ($postslist->max_num_pages);

    (without category in the args array)
    show 94 on the page.

    can you provide the full code of the template?

    please use the pastebin https://codex.www.ads-software.com/Forum_Welcome#Posting_Code

    Thread Starter synthi100

    (@synthi100)

    This is the code in my index.php file. The code to output my posts is in a different file because I also need it for a load more ajax call, but I suppose that the code to output the posts is not necessary for this issue.

    code in pastebin

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘problem with max_num_pages’ is closed to new replies.