Skip to main content
3 of 4
added 110 characters in body
nikibrown
  • 71
  • 1
  • 3
  • 10

Multisite wp_query & switch_to_blog issue

So I have a multisite install with 3 site. Site #1 is the 'main' site running on the main theme. Sites #2 and #3 run on a slightly different theme.

Sites 2 & 3 have a custom post type of 'office'. I'm trying to list the offices from sites 2 & 3 on the main site.

I'm also using advanced custom fields to chunk up all the information for the offices.

I have the page displaying the list of offices via two wp_queries. These two queries are exactly the same but one query returns some of the custom fields and the other query.

Here's the relevant code http://pastebin.com/m6N944Lq (site 2 is first, site 3 starts on line 75).

<?php switch_to_blog(2); //boston offices ?>
<?php
// WP_Query arguments
$args = array (
        'post_type'              => 'office',
        'pagination'             => false,
        'posts_per_page'         => '-1',
        'order'                  => 'ASC',
        'orderby'                  => 'title',
);
// The Query
$office2 = new WP_Query( $args ); ?>
<?php if ($office2->have_posts()): ?>
<?php while ($office2->have_posts()) : $office2->the_post(); ?>
        <li class="text--center grid__item one-quarter s-one-whole xs-one whole space all-cities all-neighborhoods all-types
            <?php $getslugid = wp_get_post_terms( $post->ID, 'space-type' );
                    foreach( $getslugid as $thisslug ) {
                            echo ' ' . $thisslug->slug . ' ';
                    }
            ?>

            <?php $posts = get_field('neighborhood_name'); if( $posts ): ?>
                    <?php foreach( $posts as $p): // variable must be called $post (IMPORTANT) ?>
                            <?php echo get_the_slug( $p->ID ); ?>
                    <?php endforeach; ?>
            <?php endif; ?>

            <?php print strtolower(get_bloginfo('name')); ?>
    ">
            <?php
                    $count = 0;
                    if( have_rows('space_images') ): ?>
                <?php while( have_rows('space_images') ): the_row();
                    $count++;
                    $image = get_sub_field('space_image');
                ?>
                    <?php if ($count == 1) { // grab the first image?>
                            <a href="<?php the_field('space_neighborhood_parent'); ?>#<?php the_field('office_url_hash'); ?>">
                                    <img src="<?php echo $image; ?>">
                            </a>
                        <?php } else { // nothing -- we only want the first image to display?>
                        <?php }  ?>
                    <?php endwhile; ?>
            <?php endif; ?>
            <hgroup>
                    <?php $posts = get_field('neighborhood_name'); if( $posts ): ?>
                            <?php foreach( $posts as $p): // variable must be called $post (IMPORTANT) ?>
                                    <h6 class="hN"><?php echo get_the_title( $p->ID ); ?></h6>
                            <?php endforeach; ?>
                    <?php endif; ?>
                    <h5 class="hN">
                            <a href="<?php the_field('space_neighborhood_parent'); ?>#<?php the_field('office_url_hash'); ?>">
                                    <?php the_title(); ?>
                            </a>
                    </h5>
                    <p class="hN"><?php the_field('office_address'); ?></p>
                    <p class="hN">
                            <?php if( has_term( 'coworking-space', 'space-type', $post->ID ) ) { ?>
                                    <i class="fa fa-lg coworking-space"></i> Coworking Space
                             <?php } ?>
                             <?php if( has_term( 'shared-office-space', 'space-type', $post->ID ) ) { ?>
                                    <i class="fa fa-lg shared-office-space"></i> Shared Office Space
                             <?php } ?>
                    </p>
            </hgroup>      
    </li><!--/.room-->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif;  // end loop?>
<?php restore_current_blog(); ?>
 
 
 
<?php switch_to_blog(3); //beyond boston offices?>

<?php
// WP_Query arguments
$args = array (
        'post_type'              => 'office',
        'pagination'             => false,
        'posts_per_page'         => '-1',
        'order'                  => 'ASC',
        'orderby'                  => 'title',
);
// The Query
$office3 = new WP_Query( $args ); ?>
<?php if ($office3->have_posts()): ?>
<?php while ($office3->have_posts()) : $office3->the_post(); ?>
    <li class="text--center grid__item one-quarter s-one-whole xs-one-whole space all-cities all-neighborhoods all-types
            <?php $getslugid = wp_get_post_terms( $post->ID, 'space-type' );
                    foreach( $getslugid as $thisslug ) {
                            echo ' ' . $thisslug->slug . ' ';
                    }
            ?>
            <?php $posts = get_field('neighborhood_name'); if( $posts ): ?>
                    <?php foreach( $posts as $p): // variable must be called $post (IMPORTANT) ?>
                            <?php echo get_the_slug( $p->ID ); ?>
                    <?php endforeach; ?>
            <?php endif; ?>

            <?php print strtolower(get_bloginfo('name')); ?>
    ">
            <?php
                    $count = 0;
                    if( have_rows('space_images') ): ?>
                <?php while( have_rows('space_images') ): the_row();
                    $count++;
                    $image = get_sub_field('space_image');
                ?>
                    <?php if ($count == 1) { // grab the first image?>
                            <a href="<?php the_field('space_neighborhood_parent'); ?>#<?php the_field('office_url_hash'); ?>">
                                    <img src="<?php echo $image; ?>">
                            </a>
                        <?php } else { // nothing -- we only want the first image to display?>
                        <?php }  ?>
                    <?php endwhile; ?>
            <?php endif; ?>
            <hgroup>
                    <?php $posts = get_field('neighborhood_name'); if( $posts ): ?>
                            <?php foreach( $posts as $p): // variable must be called $post (IMPORTANT) ?>
                                    <h6 class="hN"><?php echo get_the_title( $p->ID ); ?></h6>
                            <?php endforeach; ?>
                    <?php endif; ?>
                    <h5 class="hN">
                            <a href="<?php the_field('space_neighborhood_parent'); ?>#<?php the_field('office_url_hash'); ?>">
                                    <?php the_title(); ?>
                            </a>
                    </h5>
                    <p class="hN"><?php the_field('office_address'); ?></p>
                    <p class="hN">
                            <?php if( has_term( 'coworking-space', 'space-type', $post->ID ) ) { ?>
                                    <i class="fa fa-lg coworking-space"></i> Coworking Space
                             <?php } ?>
                             <?php if( has_term( 'shared-office-space', 'space-type', $post->ID ) ) { ?>
                                    <i class="fa fa-lg shared-office-space"></i> Shared Office Space
                             <?php } ?>
                    </p>
            </hgroup>      
    </li><!--/.room-->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif;  // end loop?>
<?php restore_current_blog(); ?>

Screenshot of how wp_query displays info for site #2 listing: http://glui.me/?i=td352h9crydphu3/2015-03-06_at_5.31_PM.png/ Site 2 working example

Screenshot of wp_query errors for site #3 listing http://glui.me/?i=m2jmwy871harhnh/2015-03-06_at_5.32_PM.png/ Site 3 not working

The error on line 262 is <?php echo get_the_title( $p->ID ); ?> which is the neighborhood name (relationship field from advanced custom fields).

The second query is also not grabbing the first image (from a repeater field in ACF).

I'm not an PHP expert (i'm a designer turned front-end developer and wordpresser) and know my way around WordPress pretty well but this has stumped me and my googling skills for a good 4+ hours today.

Any help would be VERY much appreciated. Let me know if I need to clarify anything.

Thanks in advance!

nikibrown
  • 71
  • 1
  • 3
  • 10