0

I need to show the output of two wordpress queries like below.

output example:

query 1 - post 1
query 2 - post 1
query 1 - post 2
query 2 - post 2
query 1 - post 3
query 2 - post 3
...........

mycode:

<?php
$args_one = array(
        'cat'              => 7,
        'posts_per_page'   => 8
    );
$args_two = array(
        'cat'              => 10,
        'posts_per_page'   => 8
    ); 

$the_query_one = new WP_Query( $args_one );

if ( $the_query_one->have_posts() ) :
        $num = 1;
        while ( $the_query_one->have_posts() ) : $the_query_one->the_post();
                if ($num % 2 == 0) {
                        $even   .= get_template_part('mobile-template/loop');
                } else {
                        $odd    .= get_template_part('mobile-template/loop');
                }
        $num++;        
        endwhile;
        wp_reset_postdata();

endif;

$the_query_two = new WP_Query( $args_two );

if ( $the_query_two->have_posts() ) :
        $num = 1;
        while ( $the_query_two->have_posts() ) : $the_query_two->the_post();
                if ($num % 2 == 0) {
                        $even   .= get_template_part('mobile-template/loop');
                } else {
                        $odd    .= get_template_part('mobile-template/loop');
                }
        $num++;   
        endwhile;
        wp_reset_postdata();

endif; 
      echo $even . $odd;
?>

This code works fine but I need a better or WP Standard way.

3
  • That doesn't look right to me? You'll get all Q1 evens, Q2 evens, Q1 odds then Q2 odds, not the interleaving you want.
    – Rup
    Mar 13, 2020 at 22:01
  • With some complex SQL you could do this with a single query. Depending on how the MySQL driver works (I'm not sure), if you can run two queries in parallel then you could have both WP_Queries open at once and call the_post on each one in turn. But if you are going to / do need to render each query in turn I'd put the generated strings into an array or arrays and read them out in the right order rather than concatenating everything into even / odd strings like this.
    – Rup
    Mar 13, 2020 at 22:09
  • Yes my code does not get the combination I want. Maybe I need to make a array first and then create a foreach loop? Mar 13, 2020 at 22:17

1 Answer 1

1

Why not just do straight what you've wanted.

<?php
$args_one = array(
    'cat'            => 7,
    'posts_per_page' => 8,
);
$args_two = array(
    'cat'            => 10,
    'posts_per_page' => 8,
);

// will run 2 sql queries.
$posts_one = get_posts( $args_one );
$posts_two = get_posts( $args_two );

$all_posts = array();

// lets merge them into 1 array.
while ( isset( $posts_one[0] ) || isset( $posts_two[0] ) ) {

    if ( isset( $posts_one[0] ) ) {
        $all_posts[] = array_shift( $posts_one );
    }

    if ( isset( $posts_two[0] ) ) {
        $all_posts[] = array_shift( $posts_two );
    }
}
/**
 * Now we've merged them gracefully into 1 array regarless of if these arrays are equal in size or not.
 * so we can now render them in single loop.
 */

if ( count( $all_posts ) ) :
    $num = 1;
    global  $post;
    foreach ( $all_posts as $post ) :
        setup_postdata($post);
        if ( 0 === $num % 2 ) {
            get_template_part( 'mobile-template/loop-even' ); // i've added suffix -even|-odd to make it possible to render them a bit differently.
        } else {
            get_template_part( 'mobile-template/loop-odd' );
        }
        $num++;
    endforeach;
    wp_reset_postdata();

endif;
2
  • thank you. It was exactly what I wanted, I don't think there is any better. Mar 15, 2020 at 19:44
  • @shahriyar.m you can kindly mark answer as accepted, happy to help :)
    – Mikhail
    Mar 15, 2020 at 19:46

Your Answer

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

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