Home › Forums › General Issues › Multisite: Get field from main blog
Hi
I have a multisite installation, and I would like to insert all my tracking information just one place using ACF – in the main-blog.
Is it possible to make a field that can be outputted on all my sub-blogs so that I don’t have to fill out the field on every blog?
Yes, you should be able to just switch to the main blog before getting the field https://codex.wordpress.org/Function_Reference/switch_to_blog
Hi there. I managed to write som code that did the job.
In my functions.php:
// Get custom fields from main blog
if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) {
$custom_fields = '';
$main_blog = 1;
switch_to_blog( $main_blog );
$custom_fields = array(
'head' => get_field('tracking_head', 'options'),
'body' => get_field('tracking_body', 'options'),
'footer' => get_field('tracking_footer', 'options'),
'menu_html' => get_field('menu_html', 'options')
);
restore_current_blog();
return $custom_fields;
}
And in my template file, I could retrieve the contents of the fields by writing for example:
<?php echo $custom_fields['body']; ?>
Only thing that wonders me now is that I can’t use the echo command when I put it inside an include file, ie:
<php get_template_part('templatename_with_echo_customfields'); ?>
I think I’m a little confused. I don’t see a function name, but your returning a value and I don’t see where you’re calling a function, for example
// functions.php
function get_tracking_options() {
// your function code here
}
// in template
$custom_fields = get_tracking_options();
echo $custom_fields['body'];
I’m not an expert in coding… If I try your version, I get en error
Do you mean like this?
// Get custom fields from main blog
function get_tracking_options() {
if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) {
$custom_fields = '';
$main_blog = 1;
switch_to_blog( $main_blog );
$custom_fields = array(
'head' => get_field('tracking_head', 'options'),
'body' => get_field('tracking_body', 'options'),
'footer' => get_field('tracking_footer', 'options'),
'menu_html' => get_field('menu_html', 'options')
);
restore_current_blog();
return $custom_fields;
}
}
You should always return something, you will get an error if you don’t always return the array with all of the correct indexes unless you check for the existence of those indexes before you try to use them, but I wouldn’t worry about some of your checking for MS if you’re sure that it is a multisite setup.
// Get custom fields from main blog
function get_tracking_options() {
if (get_current_blog_id() != 1) {
switch_to_blog(1);
}
$custom_fields = array(
'head' => get_field('tracking_head', 'options'),
'body' => get_field('tracking_body', 'options'),
'footer' => get_field('tracking_footer', 'options'),
'menu_html' => get_field('menu_html', 'options')
);
if (ms_is_switched()) {
restore_current_blog();
}
return $custom_fields;
}
@hube2 Hi John,
I’m using your above function to grab field data for the main site of a multisite. I cannot figure out how to grab the data for a repeater field though. Is that possible?
@jennifer-reaves it should work just like any other repeater field
The topic ‘Multisite: Get field from main blog’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.