Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a page template where I first am getting the contents of the page, as input in the admin tool, then a loop of posts of a custom post type.

I need to split this loop up into the 2 newest posts ("featured") and then a second loop of all the remaining posts. I am trying to use code from the WP Codex, from this page but the second loop is pulling the contents of the page, not the rest of the first loop. What did I screw up?

 <!-- get the contents of the WP page first -->
        <?php while (have_posts()) : the_post(); ?>
            <!-- page title -->
            <h1 class="category-title"><?php the_title(); ?></h1>   
            <!-- the subtitle is the content of the page -->
            <div class="category-subtitle"><?php the_content(); ?></div>
        <?php endwhile; ?>
        <? wp_reset_query(); ?>
        <!-- end page content -->

        <div class="articles">
        <?php       
            $args = array( 'post_type' => 'onourradar', 'posts_per_page' => 2, 'orderby' => 'menu_order', 'order' => 'ASC'  );
            $my_query = new WP_Query($args);
            while ($my_query->have_posts()) : $my_query->the_post();
            $do_not_duplicate[] = $post->ID;
            //$event_date = get_post_meta($post->ID, 'event_date', true); 
            //$clean_date = date('F j Y',strtotime($event_date)); 
        ?>
           <div class="listing">
                <?php the_post_thumbnail( 'thumb', array('class' => 'featured-image') ); ?>
                <div class="listing-name"><a href="<?php the_permalink(); ?>"><?php the_title();?></a></div>
                <div class="subhead"><?php the_field('teaser'); ?></div>
                <div class="excerpt"><?php the_excerpt(); ?></div>
            </div>

        <?php endwhile;  ?>  

        <?php if (have_posts()) : while (have_posts()) : the_post(); 
          if (in_array($post->ID, $do_not_duplicate)) continue;
        ?>
           <div class="listing-name"><a href="<?php the_permalink(); ?>"><?php the_title();?></a></div>
        <?php endwhile; endif; ?>

EDIT: This second loop seems to work, not sure if this is the proper way. Suggestions appreciated:

 <?
            $args = array( 'post_type' => 'onourradar', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'post__not_in' => $do_not_duplicate );
            $my_query = new WP_Query($args);
            while ($my_query->have_posts()) : $my_query->the_post();
        ?>
share|improve this question
1  
Not sure if this is the best way, but I modified the second loop to duplicate the query except for posts in my do_not_duplicate array. Or is there a better way? –  Steve Dec 31 '12 at 15:15
add comment

2 Answers

up vote 0 down vote accepted

Your second loop is fine because it works - although it is probably more efficient to do it this way:

$args = array( 'post_type' => 'onourradar', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'offset' => '2');
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();

The difference is that a do_not_duplicate query is just not a very efficient way to do things (although sometimes it is necessary). Using the offset is definitely a better way to go.

share|improve this answer
add comment

If I am reading your question right, you only need one query. Here the bare-bones technique.

$q = array('posts_per_page'=>10);
$a = new WP_Query($q);
$count = 0;
while ($a->have_posts()) {
  $a->the_post();
  echo $post->ID."</br>";
  $count++;
  if ($count == 2) break;
}
echo '##########################</br>';
while ($a->have_posts()) {
  $a->the_post();
  echo $post->ID."</br>";
  $count++;
}

I think you should be able to incorporate that example into your own code. You print the first two posts (or any number you want) and if you don't reset the query you can pickup where you left off later.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.