• Hi guys,

    I’m trying to create custom blog template for my theme. And I want to show one post per page (it’s whole blog post template). The problem I have is that even ‘posts_per_page’ is set to 1 it still shows two posts on first page, the rest of it is fine. Any ideas how to solve this issue? Thanks.

    So here is my code
    $args = array(
    ‘post_type’ => ‘post’,
    ‘posts_per_page’ => 1,
    ‘paged’ => $paged,
    ‘category_name’ => $categories,
    ‘order’ => $order,
    ‘orderby’ => $orderBy,
    );

    $query = new WP_Query($args);

    if ( $query->have_posts() ) :
    while ( $query->have_posts() ): $query->the_post();
    // show post
    the_title();
    the_content();
    endwhile;
    endif;
    wp_reset_postdata();
    // pagination
    if($query->max_num_pages > 1):
    $tempQuery = $wp_query;
    $wp_query = NULL;
    $wp_query = $query;
    $big = 999999999;
    $args = array(
    ‘base’ => ‘%_%’,
    ‘format’ => ‘?paged=%#%’,
    ‘prev_text’ => “”,
    ‘next_text’ => “”,
    );
    ?>
    <nav class=”blog-navigation text-center”>
    <?php echo paginate_links($args); ?>
    </nav>
    <?php
    $wp_query = NULL;
    $wp_query = $tempQuery;
    endif;

Viewing 3 replies - 1 through 3 (of 3 total)
  • i think it is -1

    Moderator bcworkz

    (@bcworkz)

    -1 posts per page causes all posts to be returned.

    Do you have a sticky post? They are not counted in the posts per page. There’s an argument to ignore sticky posts, but then of course the sticky will not be the first post anymore.
    'ignore_sticky_posts' => true,

    Thread Starter kajdzo

    (@kajdzo)

    @bvworkz It was the sticky post, didn’t noticed it. I imported WP test posts and didn’t even noticed that there is a sticky post. Didn’t know for this argument. Thanks very much, really helped me.

    • This reply was modified 8 years, 2 months ago by kajdzo.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Help With WP_Query and posts_per_page’ is closed to new replies.