1

I've a custom WP_Query something like this:

$query_args = array(
    'post_type'         => 'post',
    'cat'               => 12,
    'posts_per_page'    => 5
);

$custom_query = new WP_Query($query_args);

Now, I need to split the results of this query in 3 sets, first set will have the first post only, the second set will have 2nd and 3rd posts and then the third set will have 4th and 5th posts.

The reason for this split is that entries in each set will have different outer and inner markup, outer markup is something like this:

<div class="column" data-span="6">1st Post Here</div>   

<div class="column" data-span="3">2nd + 3rd Posts Here</div>

<div class="column" data-span="3">4th + 5th Posts Here</div>

Is there is any way to print 1st post in the first div, pause the query, then resume the query in the second div, pause and the further resume the query in the third div?

1

I think the best way to approach this might be to run your WP_Query just one time, but then access the stored query results three times to output each wanted set of data. This will save you from using multiple DB queries, and by using three separate loops, you can still add a parent div to each set of posts as desired. Here's the concept I came up with:

<div id="main-content">

    <?php 
        $q = new WP_Query([
            'post_type' => 'post',
            'posts_per_page' => 6
        ]);

        $posts = $q->posts;
    ?>

    <div id="set_1" class="column" data-span="6">
    <?php
        $c = 0;
        foreach ($posts as $a_post) {
            $c++;
            if( $c==1 ) {
                echo $a_post->post_title . " - Post #$c<br>";
                break;
            }
        }
    ?>
    </div>

    <div id="set_2" class="column" data-span="3">
    <?php
        $c = 0;
        foreach ($posts as $a_post) {
            $c++;
            if( $c==2 || $c==3 ) {
                echo $a_post->post_title . " - Post #$c<br>";
            }
        }
    ?>
    </div>

    <div id="set_3" class="column" data-span="3">
    <?php
        $c = 0;
        foreach ($posts as $a_post) {
            $c++;
            if( $c==4 || $c==5 ) {
                echo $a_post->post_title . " - Post #$c<br>";
            }
        }
    ?>
    </div>

</div><!-- end #main-content -->
0

You can write html conditionally, using if statement to check index and printing html based on that.

Something like:

$custom_query = new WP_Query($query_args);
$i = 1;
while($custom_query->have_posts()): the_post();
    if($i == 1){?> 
        <div class="column" data-span="6">1st Post Here</div>   <?php }
    elseif($i == 2 || $i == 3){?> 
        <div class="column" data-span="6">1st Post Here</div>   
    <?php }
    else{?> 
        <div class="column" data-span="6">1st Post Here</div>   
    <?php }
endwhile;

If only difference is data-span in your html, you can skip second condition and use

<div class="column" data-span="<?php echo ($i==1)?6:3;?>">your Post Here</div>
  • That's the issue, I can apply if condition within while() statement but then it'll wrap EACH post with column div wrapper, whereas, I only need this wrapper around each SET of posts, a set can contain multiple posts. – Faisal Khurshid May 18 '17 at 4:53
  • This will be little complex logic then, but I think it could be solved using if's – Aniruddha Gawade May 18 '17 at 5:12
  • It can be solved by using if condition for each post and printing appropriate markup tags accordingly but the code would be pretty ugly and would be hard to maintain. I would prefer a simpler and better solution. – Faisal Khurshid May 18 '17 at 5:17
  • Instead of incrementing a variable, the query actually knows where you are in the loop. Check $custom_query->current_post for the current post counter. Remember, the counter starts at zero, not at one. – Otto Jan 11 '18 at 15:28
0

Try this:

You will use the variable $gottenposts to save the printed posts ids.Then you will make a new query when you want to show the other posts excluding the posts already printed.

Your first query

<?php $gottenposts = array(); //here you will save the posts ids
$args = array('post_type' => 'post', 'posts_per_page' => 6); 
$query = new WP_Query($args); 
if($query->have_posts()) : 
 while($query->have_posts()) : 
   $query->the_post();?>
 //your HTML code here
<?php endwhile; endif; ?>

Your second query

<?php $args = array('post_type' => 'post', 'posts_per_page' => -1, 'post__not_in' => $gottenposts ?>
//ETC...

Hope this helps!

  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Jan 8 '18 at 17:20
-1

I think you can't do this with single query. You have to use multiple query with post number.

It would be better using custom field slug.

$slider = new WP_Query(array(
        'post_type' => 'post',
        'posts_per_page' => 5,
        'cat'               => 12,
        'post_status'   => 'publish',
         'meta_query' => array(
                     array(
                        'key' => 'slider_key',
                        'value' => 'slider',
                        'compare' => 'IN'
                    ) 
                )


));
  • Setting custom field is not an option here. – Faisal Khurshid May 18 '17 at 4: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.