Dynamically populate an ACF (radio) field on a multi-user installation

I am trying to populate the Advanced Custom Fields field by pulling data from a custom post type located in the main blog of a tiered installation.

For a better understanding, I made this simple flow chart.

Flow

So, I created a function to pull data from the main blog and show how the radio elements are on the subsidiary site.

The function looks like this and I used this as a reference

function getctas($field) { $field['choices'] = array(); switch_to_blog(1); $args = array( 'post_type' => 'location_icons', 'posts_per_page' => '-1', ); $ctas = new WP_Query( $args ); while ( $ctas->have_posts()) { $ctas->the_post(); $choices = get_field('icon',false); $choices = explode("\n", $choices); foreach( $choices as $choice ): $field['choices'][ $choice ] = '<img src="'.$choice.'"/>'; endforeach; } restore_current_blog(); return $field; } add_filter('acf/load_field/name=call_to_action_icon', 'getctas'); 

I correctly understood the parameters (options are images), I successfully pulled out the icon field from the main blog and placed the radio and the value as a label.

The problem I am facing is that after the message is saved, when I request it on the child page template, I get the correct images, but the title of the blog post 1 is repeated. Ideal would be:

  • Picture
  • Blog Title
  • Children's Blog Post Desc

And what I get instead is:

  • Correct image
  • CTP header containing blog image 1
  • No description

  • Correct image

  • Same name as previous.
  • No description

And so on.

If any of you need further clarification to help me solve this problem, I will be happy to explain further.

+7
wordpress advanced-custom-fields
source share
1 answer

Please declare global $switched; before going to the main blog, it seems that wp does not switch it back to the current blog if it does not work after declaring a global variable. Please, try

get the current blog id before going to the main blog $current_site =get_current_blog_id();

as soon as you are done. return it using

 switch_to_blog( $current_site ); $GLOBALS['_wp_switched_stack'] = array(); $GLOBALS['switched'] = FALSE; 
+1
source share

All Articles