• Resolved dgcov

    (@dgcov)


    I’m not sure what’s going on here.

    This is my code:

    $events=new WP_Query( array('post_type'=>'team_list_event') );
      if($events->have_posts()){
        while($events->have_posts()){
          $QID=$events->post->ID;
          echo '
          <tr><td>
          <a href="">'.get_the_title($QID).'</a>
          </td></tr>';
          $events->the_post();
          $i=($i==0)?1:0;
        }
        echo '</table>';
      }

    I have 2 posts, ID 6 and ID 22.

    ID 6 is skipped and ID 22 is rendered twice.

    I’ve obviously misunderstood how the loop works: what am I doing wrong?

    I suppose I could set up a query “SELECT ID,post_type FROM wp_posts WHERE post_type= ‘team_list_event'”, but I want to do it the proper way.

Viewing 2 replies - 1 through 2 (of 2 total)
  • move this line:
    $events->the_post();

    directly to the location after this line:
    while($events->have_posts()){

    Thread Starter dgcov

    (@dgcov)

    Thanks!

    In my defence, isn’t that a little counter intuitive?

    I would expect to wind on the post after I’d accessed the content rather than before.

    But it’s a minor gripe, so long as it works! And I’m sure it’s tripped up others besides myself.

    🙂

    Thanks again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_query omitting posts and returning duplicates.’ is closed to new replies.