1

3/17/2015 update

So it appears that this is a known issue with ACF and multisite using switch_to_blog.

http://support.advancedcustomfields.com/forums/topic/multisite-switch_to_blog-options-issue/

Exporting the fields to my sub-sites within the plugin worked. Hardcoding the custom fields into the functions.php file does not seem to work for multisite.


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!

  • I should also note that I'm totally game to cough up $$$ for help :) – nikibrown Mar 6 '15 at 22:41
  • How come you are looping through the custom fields? They all appear to be single items, so it's hard to figure out why they are repeaters. Also, you're checking for $posts but not for $p. So it's possible you're looping through something that shouldn't be looped through. I'd var_dump or print $posts to see what's up. – Brian Krogsgard Mar 6 '15 at 23:53
  • You should add your code and images inside your question. It is extremely irritating opening 4 different screens to read one question. The very low response rate might be due to this as most simply skip and ignore such questions. Also, in the bigger picture, if those links ever die or gets removed, your question will be useless to future users as it will have no context anymore. Please file an edit with the requests – Pieter Goosen Mar 7 '15 at 4:34
  • Just to add, looking at your previous questions, you have received a couple of good answers, but you haven't left any feedback on them. Please revisit them all, and accept answers that solved your issue. This way, that questions will not get rotated by the system indefinitely, and those questions can serve as proper referrence to others, and most of all, that questions will be marked as resolved. Failing to leave feedback make others tend to ignore any future questions from you as they feel they are wasting their time answering your questions. :-) – Pieter Goosen Mar 7 '15 at 4:40
  • @BrianKrogsgard I'm looping through as its a relationship field - see documentation for ACF here: advancedcustomfields.com/resources/relationship I'm confused as to why it works for site#2 but not site#3. Same custom fields and theme (hard coded into functions.php). Also can you explain how to do a var dump? – nikibrown Mar 10 '15 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.