0

I have a very annoying issue with 2 wp_query.

  1. Showing 1 post - feature post

     <?php 
     $do_not_duplicate = array(); 
    
    
    
     $args = array (
    'post_type'  => 'post',
    'posts_per_page'  => '1',
    
    );
    
    $my_query = new WP_Query( $args );
    if ($my_query->have_posts()) :
    while($my_query->have_posts()) :
    $my_query->the_post();
    $do_not_duplicate[] = $post->ID; 
    get_template_part( 'content-feat',get_post_format() );
     endwhile;
    endif;
    wp_reset_postdata(); 
    
    ?>
    
  2. The rest of the posts

    <?php 
    
    
            // the query to set the posts per page to 6
        query_posts($args);
        $args = array(
            'posts_per_page' => 6, 
            'paged' => $paged,
    
            );
    
    
        $my_query = new WP_Query( $args );
        if ($my_query->have_posts()) :
        while($my_query->have_posts()) :
        $my_query->the_post();
            if ( !in_array( $post->ID, $do_not_duplicate ) ) { // check IDs         
    
                get_template_part( 'content', get_post_format() );
            }
    
            endwhile;
            endif;
    
         ?>
    

The problem:

Second loop is removing the post from first query (like its suppose to do) and should show 6 posts.

The problem is that is showing on first page 5 posts and on second 6 posts. I don't know where is the problem, can u guys help me? I'm kinda stuck with this issue. Thx

1
  • 1
    what you describe is exactly what I'd expect it to do. what do you expect to happen?
    – Milo
    Commented May 2, 2014 at 0:32

1 Answer 1

3

Your second block of code is strange. The use of query_posts() doesn't really make sense. I don't even know what that is intended to do. Additionally, that is not really the best way to exclude posts from a Loop, and is what is causing your 5 vs. 6 posts issue.

You need to exclude the "do not duplicate" post with query arguments.

$args = array(
  'posts_per_page' => 6, 
  'paged' => $paged,
  'post__not_in' => $do_not_duplicate,
);

$my_query = new WP_Query( $args );
if ($my_query->have_posts()) {
  while($my_query->have_posts()) {
    $my_query->the_post();
    get_template_part( 'content', get_post_format() );
  }
}

What is happening that is causing confusion is that your query returns 6 results but that includes the post you want to exclude. When you exclude that in your Loop, you end up with only 5 posts.

0

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.