Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically populate an ACF field (radio) on a multisite install

I'm trying to populate an Advanced Custom Fields' radio field pulling out the data from a custom post type located in the main blog of a multisite install.

For a better understanding I made this simple flow graphic.

Flow

So I created a function in order to pull out the data from Main Blog and show as radio items on child site.

The function looks like this and I used this as 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 get the options listed correctly (options are images), I successfully pulled out the field icon from main blog and put as radio label and value.

The issue I'm having is that once the post is saved when I query it on the child's page template I get the correct images but the title of the post on blog 1 repeated. The ideal would be to have:

  • Image
  • Child Blog Post's Title
  • Child Blog Post's Desc

And what I'm, instead getting is:

  • Correct Image
  • CTP Title that contains the image on blog 1
  • No description

  • Correct Image

  • Same title as previous one
  • No description

And so on.

If any of you need more clarifications to help me solve this I'd be pleased to explain further.

like image 737
Jaypee Avatar asked Jun 03 '14 19:06

Jaypee


1 Answers

Please declare global $switched; before switching to the main blog , it seems wp is not switching it back to the current blog if it does not work after declaring the global variable, Please try this

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

once you are done. switch it back using

switch_to_blog( $current_site );
$GLOBALS['_wp_switched_stack'] = array();
$GLOBALS['switched']           = FALSE; 
like image 100
Deepti chipdey Avatar answered Sep 27 '22 18:09

Deepti chipdey