I'm looking for some advice on how to make a gallery of images. it is a particular bit a gallery, because I would show the last 5 post and a fixed post, for a total of 6 posts, in a specific order. ie the fixed post must be the third in the list. The only method that I believe it is possible to create 3 loops, the first to see the last two post, the second to display the fixed post (by id), and the third to see other last three posts. Something like this (most probably it is not correct):

        <?php $my_query = new WP_Query( 'posts_per_page=2' ); // get the latest 2 posts
            while ( $my_query->have_posts() ) : $my_query->the_post();
            $do_not_duplicate = $post->ID; 
            <?php the_title(); ?>

        <?php endwhile; ?>

        <?php $my_query2 = new WP_Query( 'p=1' ); // get fixed post with id=1
            while ( $my_query2->have_posts() ) : $my_query2->the_post();
            <?php the_title(); ?>

        <?php endwhile; ?>

        <?php $my_query3 = new WP_Query( 'posts_per_page=3' ); // get the latest 3 posts
            while ( $my_query3->have_posts() ) : $my_query3>the_post();
            if ( $post->ID == $do_not_duplicate ) continue; // don't show the same post of the first loop

            <?php the_title(); ?>

        <?php endwhile; endif; ?>

There's a different way to do this without do many loops, considering that there's already one in the page ?

thank you


I cannot think of a way of getting the latest 5 posts plus a specific one in a single query without using SQL.

If you don't mind using two queries, I think this would be an easy way to do what you intend:

//Get the latest 5 posts (which happens to be get_posts default 'posts_per_page' value)
$posts = get_posts();

//Build up an array with the IDs
$latests_posts_ids = wp_list_pluck( $posts, 'ID' );

//Insert the desired ID in the third position
$post_id_to_insert = 1;
$insert_position = 2; //which equals 3 when starting from 0
array_splice( $latests_posts_ids, $insert_position, 0, $post_id_to_insert );

//Get the posts based on the IDs and do not alter their order
$args = array(
    'posts_per_page' => 6,
    'post__in' => $latests_posts_ids,
    'orderby' => 'post__in'

$posts = get_posts( $args );

//Parse the posts
foreach( $posts as $post ) :

    setup_postdata( $post );


I'm using get_posts() but you can safely use WP_query instead.

WordPress already handles possible duplicates in post__in, which would be the case if the post you want to insert is one of the latest 5.

| improve this answer | |
  • Let me know if it works for you. – Luis Sanz May 25 '16 at 17:23
  • your code doesn't work if there'r multiple loop on the same page. The second loop is like the first. Any suggestions ? – sb0k Aug 8 '16 at 15:12
  • @sb0k, use wp_reset_postdata() between both. – Luis Sanz Aug 8 '16 at 22:42
  • i closed the first loop with endofeach; wp_reset_postdata(); & wp_reset_query();. but nothing... – sb0k Aug 9 '16 at 8:20
  • What are your arguments for get_posts() on the second loop? As it's in my answer, it will retrieve the five latest posts. You might need to change its query parameters to achieve a different result. – Luis Sanz Aug 9 '16 at 17:12

One way would be to use the built in counter to tell how many times you've been through the loop, then use get_post on the fixed posts id once you've been through 3 times...

        $my_query = new WP_Query( 'posts_per_page=5' ); // get the latest 5 posts
        while ( $my_query->have_posts() ) : $my_query->the_post();
        $do_not_duplicate = $post->ID; 
        <?php the_title(); ?>

         if(3 == $my_query->current_post) {
             $fixed_post = get_post(1);
             // Do what you need to with $fixed_post...

See the wordpress developer reference for get_post for more details...

(Editted code to use built in counter as per suggestion of Milo in comments.)

| improve this answer | |
  • 1
    you can also use the counter built into every query object, $my_query->current_post – Milo May 24 '16 at 15:26
  • Even better. I'll update answer... – AntonChanning May 24 '16 at 15:28
  • thank you so much.. is a solution which I had not thought. I'm sure that will give good results. – sb0k May 24 '16 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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