How do I Use Nested Loops of Custom Post Types for MultiSite Blogs using WP_Query()

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!

1 Answer
1

This Plugin gives you a list of posts for each Site already, but it does not exactly do what you want to do.

Basically, it gives you a list of all posts in your network. What you need to do afterwards is sort them by site_id and create an output for that.

I do not know if this is the best version – but as WP_Query returns an one-dimensional array, you need something else to group and sort it by site_id

$args_outer = array(
    'multisite' => 1,
    'sites__not_in' => array(1),
    'post_type' => 'my_cpt',
    'numberposts' => -1,
    'posts_per_page' => -1
);
$child_site_query = new WP_Query_Multisite( $args_outer );

while($child_site_query->have_posts()) : $child_site_query->the_post();
    global $blog_id, $post;
    $blogs[$blog_id][] = $post; // create array with first parameter blogid, and the posts inside
    // --------------------------------------------------
endwhile;  // END: Outer Loop
wp_reset_postdata();     

// loop through all blogs
foreach( $blogs as $thisblog => $posts ) {
    // switch to this blog and create output
    switch_to_blog( $thisblog );
    echo 'Site ' . $thisblog . '<br /><ul>';
    // loop through all posts on this blog
    foreach( $posts as $thispost ) {
        echo '<li><a href="' . get_permalink( $thispost->ID ) . '">' . $thispost->post_title . '</a></li>';
    }
    echo '</ul>';
}
restore_current_blog();

Leave a Comment