I’m using the code from miguelpeixe’s ‘WordPress Query Multisite’ in order to create a custom loop for my WP Multi-site’s ‘child sites.’ Within this loop, I want to nest another loop that will pull custom post types from that child site, like:
Site 1
CPT 1
CPT 2
Site 2
CPT 3
CPT 4
I am actually able to get the loop working and able to pull the Child Custom Post Types. However, The loop is repeating one child site. I’m not sure if I’m just missing some obvious bug or what, here’s the code I’m using:
<?php
$args_outer = array(
'multisite' => 1,
'sites__not_in' => array(1),
);
$child_site_query = new WP_Query( $args_outer );
while($child_site_query->have_posts()) : $child_site_query->the_post(); global $blog_id; // START: Outer Loop
// --------------------------------------------------
switch_to_blog($blog_id);
$args_inner = array(
'post_type' => 'my_cpt'
);
$child_cpts_query = new WP_Query( $args_inner );
while($child_cpts_query->have_posts()) : $child_cpts_query->the_post(); // START: Inner Loop
// Variables
$child_cpt_id = get_the_ID ();
$child_cpt_thumbnail = get_post_meta( $child_cpt_id, '_thumbnail_id', true );
$child_cpt_permalink = get_the_permalink();
?>
<!-- START: HTML -->
<div class="col-md-3">
<h3>
<a href="https://wordpress.stackexchange.com/questions/216836/<?php echo($child_cpt_permalink);?>">
<?php echo $blog_id; ?><br>
<?php echo $child_cpt_id; ?> -
<?php echo get_the_title(); ?>
</a>
</h3>
</div>
<!-- END: HTML -->
<?php
endwhile; // END: Inner Loop
wp_reset_postdata();
restore_current_blog();
// --------------------------------------------------
endwhile; // END: Outer Loop
wp_reset_postdata();
?>
Am I messing up with regard to wp_reset_postdata() or restore_current_blog() on the inner loop? (Which is my suspicion).
Thanks in advance for any insights!