Support

Account

Home Forums General Issues Multisite switch_to_blog Options issue

Solved

Multisite switch_to_blog Options issue

  • Hello! I have a Multisite install and use <?php switch_to_blog(1); ?> and <?php restore_current_blog(); ?> to get content (menu, slider…) from the mail site to show up on sub-sites.

    Everything works great with ACF when it comes to getting information from general field groups… but I am having problems when it comes to options pages. No meta content is being retrieved and nothing is showing up on the sub-sites.

    Are there any known issues about this or some solution?

    Any help is greatly appreciated!

  • Hi @Full Circle Design

    Perhaps you could post the code yo use to switch and load options page data?

  • Thank you for the quick response! Sure thing. The full piece of code I’m using is to pull the header from my main site to my sub-sites. The particular bit in question is this piece:

           <?php switch_to_blog(1); ?>
    	<?php if (get_field('activate_alert', 'option')) { ?>
    		<div id="globalAlert">
    			<div class="content"><?php the_field('manage_alert', 'option'); ?></div>
    		</div>
    	<?php } ?>

    The field activate_alert is a true/false field. So when the user checks it off as true, I want to display the manage_alert field, which is a basic wysiwyg field.

    The entire portion of code is as follows (so you can see where I’m placing the alert field in relation to the entire header):

    <body <?php body_class(); ?>>
    
    <?php switch_to_blog(1); ?>
    	<?php if (get_field('activate_alert', 'option')) { ?>
    		<div id="globalAlert">
    			<div class="content"><?php the_field('manage_alert', 'option'); ?></div>
    		</div>
    	<?php } ?>
    
    <div id="page" class="hfeed site">
    	<?php do_action( 'before' ); ?>
    	<div id="headerWrap">
    		<header id="masthead" class="site-header" role="banner">
    			<div id="imagebanner"></div>
    			<div class="site-branding">
    				<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
    				<?php $text=get_bloginfo('name'); $text=explode(' ',$text); $text[0]='<span>'.$text[0].'</span>'; $text=implode(' ',$text); echo $text; ?>
    				</a></h1>
    			</div>
    			
    			<div id="searchform"><?php global $global_site_search; echo $global_site_search->global_site_search_search_form_output('',''); ?></div>
    	
    			<nav id="site-navigation" class="navigation-main" role="navigation">
    				<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
    			</nav><!-- #site-navigation -->
    
    			<nav id="secondary-navigation" class="navigation-secondary" role="navigation">
    				<?php wp_nav_menu( array( 'theme_location' => 'quicklinks', 'menu_class' => 'quicklinks', 'items_wrap' => '<ul id="%1$s" class="%2$s"><li><a href="#">Quick Links</a><ul>%3$s</ul></li></ul>' ) ); ?>
    				<?php wp_nav_menu( array( 'theme_location' => 'webconnect', 'menu_class' => 'webconnect', 'items_wrap' => '<ul id="%1$s" class="%2$s"><li><a href="#">Web Connect</a><ul>%3$s</ul></li></ul>' ) ); ?>
    			</nav><!-- #site-navigation -->
    
    		</header><!-- #masthead -->
    	</div>
    	<?php restore_current_blog(); ?>
    
  • Hi @Full Circle Design

    Shouldn’t the restore_current_blog function run before the:

    
    <div id="page" class="hfeed site">
    

    ?

  • Hello again!

    I am actually pulling that entire chunk of code from my main site. The restore_current_blog function is at the very bottom of the full piece of code.

    I’m essentially creating a global header across my multisite network from the main site.

  • Hey Elliot! I spent this morning really trying to hone in on what the issue was, and I realize it was a very simple thing I didn’t think to mention.

    I had exported the ACF field groups I created to hardcode them inside my theme’s function file, including the option page’s field groups. It seems like using multisite and any custom field that is generated inside the functions.php file will not pull information correctly.

    I simply reactivated and recreated my field groups inside the plugin interface on my website and everything works perfectly now. All fields and all options.

    I hope this can help anyone else experiencing issues with ACF and multisite.

    Thank you again Elliot for an amazing plugin and all your hard work!

  • I see that re-creating the fields using the plugin’s interface solved this problem for Full Circle Design. However, I have a Multi-Sites setup which has one main site and 25 satellite sites (and growing) which all will feed certain data into the main site. The satellite sites need to be setup quickly and the same way every time, so I need the fields to be created in the functions.php file. However I still need to access the field values from the main site, using switch_to_blog and a custom WP_Query.

    Elliot, do have any suggestions for how I can access the custom field data in this situation?

  • Hi @jzsouthern

    Sorry, I don’t understand what the issue is here. If your fields are registered via PHP and use the same field_key’s, there should be no issue with loading data with the switch_to_blog function…

    Thanks
    E

  • I hope it is okay to hook into this discussion.

    I experience the same issue. Hardcoded Fields won’t give me the option page values from sub-sites after the switch_to_blog(), but ui-created fields work great.

    I tried to debug it. The created query is alright but it gives ACF no data back.

    SELECT option_value
    FROM sw_4_options
    WHERE option_name = ‘options_standort_color_main’
    LIMIT 1

  • I was able to solve the issue and move on with my life by using the built-in WordPress function get_post_meta()

    http://codex.wordpress.org/Function_Reference/get_post_meta

    Not as nice as working with get_field() and the_field(), but at least it’s reliable in a Network situation.

    Hope that helps!

  • I have run into this issue as well. Have not tried the get_post_meta (this gets messy with repeaters!!!).

    I posted on stack overflow and had another developer help me troubleshoot.

    http://wordpress.stackexchange.com/questions/180424/multisite-wp-query-switch-to-blog-issue

    I’m going to try to reproduce this on a fresh multisite install with a simplified test case.

  • This is an older question but I thought I’d chime in with what I believe was the issue here and this question ranks high in the searches.

    In a multi-site situation where you’re hardcoding the fields via PHP you need to make sure those options are being initialized on all the sub sites as well. If you have a global options page on the primary site within a Multisite network but those options aren’t also initialized on the sub sites, you’ll get the above behavior. Specifically the data for the field is accessible but it doesn’t bring in any of the ACF-specific formatting. For example, a repeater field will just return the number of sub-items, not the actual content of the sub-items.

  • @hereswhatidid having the same exact problem — I get the repeater row count rather than the saved data.

    Fields are initialized on all sites, and if I populate the fields on each site, the field works just fine. If I leave the field blank on each site, but populate it on the main site, it returns the row count on everything but the main site.

    Only weird thing for me is that I export the JSON and load in the fields using PHP, but I can’t imagine that would make any difference…

  • Got a bit more info — it seems like the fields actually display correctly if I *dont* have the fields registered on my child sites. On sites that don’t have the fields, the main site data comes through correctly, but on sites that do, the main site data is misformatted.

    EDIT: Or maybe not, it just seems super sporadic. I just re-enabled the fields on all sites, and nothing changed. It seems like not all fields are affected on all sites, it’s like a random mix of some fields working and some not… I have a WYSIWYG field that’s working fine on 4/5 child sites, but unformated on 1, and a repeater that doesn’t work on any. Very odd.

Viewing 15 posts - 1 through 15 (of 15 total)

The topic ‘Multisite switch_to_blog Options issue’ is closed to new replies.