1

On the page that displays a single post, I also wish to display some featured posts.

The featured posts have a meta value assigned to them to differentiate between featured and non featured posts.

The problem is that I wish to display the featured posts half way down my page but the loop starts at the top and doesn't finish until the bottom of the page.

From WP docs:

<?php
    // The main query.
    if (have_posts()) {
      while (have_posts()) {
        the_post();
        the_title();                                                             
        the_content();                                                          
      } # End while loop                                                             
    } else {                                                                      
        // When no posts are found, output this text.                           
        _e( 'Sorry, no posts matched your criteria.' );                         
    }                                                                  
    wp_reset_postdata();                                                        

    /*                                                                          
     * The secondary query. Note that you can use any category name here. In our example,
     * we use "example-category".                                               
    */                                                                        

    $secondary_query = new WP_Query( 'category_name=example-category' );        

      // The second loop. 
    if ($secondary_query->have_posts()) {
      echo '<ul>';
        // While loop to add the list elements.
        while ($secondary_query->have_posts()) {
            $secondary_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
      echo '</ul>';
    }
wp_reset_postdata();
?>

At the end of the first loop, you have to call wp_reset_postdata() but in my scenario, there is data that needs to be retrieved further down the page so I can't end it there.

I essentially need to do this but then only the featured posts get rendered and not the post itself.

if (have_posts()) {

    while (have_posts()) { 
      the_post();
      the_title();                                                             
      the_content();                                                         

      //Display featured posts half way through
      $secondary_query = new WP_Query( 'category_name=example-category' );
      //end featured post loop
      wp_reset_postdata(); 

      //continue outputting data from first loop 
      the_title();
    } # End while loop.                                                                 

} else {                                                     
    // When no posts are found, output this text.                           
    _e( 'Sorry, no posts matched your criteria.' );                         
}

//finally end inital loop                                                        
wp_reset_postdata(); 

Is it possible to 'pause' the loop to do a different loop then pick it back up again later on?

1 Answer 1

2

Normaly you second code example should work. You don't have to call wp_reset_postdata() to end the main loop, call it only to end the secondary loop.

Use this function to restore the global $post variable of the main query loop after a secondary query loop using new WP_Query. It restores the $post variable to the current post in the main query.


You can also use get_posts() for this:

$secondary = get_posts( array(
    'posts_per_page'   => 5,        
    'category_name'    => 'example-category',
    'orderby'          => 'date',
    'order'            => 'DESC',       
    'meta_key'         => 'featured_posts',
    'meta_value'       => 'yes',
    'post_type'        => 'post',       
    'post_status'      => 'publish',        
) );

if ( count( $secondary ) ) {
    echo '<ul>';
    foreach ( $secondary as $entry ) {
        // print_r( $entry ); exit;
        echo '<li>' . $entry->post_title . '</li>';
    }
    echo '</ul>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.