0

The idea seems simple enough, insert a post from a different post type after every 3rd post in a wp_query. So should look something like this:

Article 1
Article 2
Article 3
Ad 1
Article 4
Article 5
Article 6
Ad 2
Article 7
Article 8
Article 9
Ad 3

In the example above, the two different post types are 'Article' and 'Ad'. I'm able to get the basic setup using modulo operator, using either 2 wp_queries, or with a single one like in the code example below. The issue I'm running into is that I can't seem to get the Ad loop to iterate correctly, when using two different loops I'm not able to pass the 2nd Ad after the 6th Article, it will just all dump at the 1st Ad position ($counter = 4)

<?php

//   $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$get_posts = new WP_Query(array(
    'post_type'         => array ( 'articles', 'ads' ),
    'post_status'       =>'publish', 
    'posts_per_page'    =>-1
));

$counter = 1;

if ( $get_posts -> have_posts() ) : 
    while ( $get_posts -> have_posts() ) : $get_posts -> the_post();

        if ( $counter % 3 === 0 && $counter > 1 ) {
            if ( get_post_type() == 'ads') {
    
                echo the_title();
        
            }
        }

        $counter++;

    endwhile;

else : ?>
    <p class="notice"><?php _e( 'There are no posts to display at the moment.' ); ?></p>
<?php endif; ?>

Is it correct to run both post types through a single loop, or should I break them apart into different loops to correctly iterate? Thanks.

1 Answer 1

0

Figured out a solution that works for the issue I was having. This should be able to place an 'Ads' custom post type every 3rd post of 'Articles' custom post type.

<?php

    //   $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $get_posts = new WP_Query(array(
        'post_type'         => 'articles',
        'post_status'       =>'publish', 
        'posts_per_page'    =>-1
    ));

    $posts = get_posts(array(
        'post_type' => 'ads',
        'posts_per_page' =>-1,
        //'orderby'   => 'date'
      ));
      
    $master = [];
    $counter = 1;

    if ( $get_posts -> have_posts() ) : 
        while ( $get_posts -> have_posts() ) : 
            $get_posts -> the_post();

                echo the_title();

                if ($counter % 3 === 0 ) {

                        foreach ($posts as $post) {
                            setup_postdata($post);
                        
                            $title = $post->post_title;
                            $post_id = $post->ID;
                            $master[$title] = $posts[($counter-1)/3]->post_title;
                    
                        }
                      wp_reset_postdata(); 

                    if ($master[$title]) {
                        $master[$title];
                    }
                }
                $counter++;
        endwhile;

    else : ?>
        <p class="notice"><?php _e( 'There are no posts to display at the moment.' ); ?></p> <?php 
    endif;

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.