Why is this hook for a ACF Multisite Cross-Site Select field causing issues?

16 Views Asked by At

I have the code below to populate an ACF Select field from a Custom Post Type on another Site in a Multisite Network. (CPT = 'cta', fieldname = 'site1_cta')

It works, ie it populates the select and allows the rest of ACF to work it's magic but it causes issues: One issue is that if the Select Field is positioned in the Side the content of some fields in the main body disappear.

So it works from the point of view of development but its not ready to move to a live site.

Can anyone see anything in this code that would cause issues with other ACF Field Groups (or anything else)

function acf_load_cta( $field ) {

    $field['choices'] = array();
    $language = pll_current_language();

    switch_to_blog(1);

    $args = array(
        'post_type' => 'cta',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'lang' => $language,
    );

    $page_query = new WP_Query($args);

    if ($page_query->have_posts()) {
        while ($page_query->have_posts()) {
            $page_query->the_post();
            $field['choices'][get_the_ID()] = get_the_title();
        }
    }

    wp_reset_postdata();
    restore_current_blog();
    // return the field
    return $field;
}

add_filter('acf/load_field/name=site1_cta', 'acf_load_cta', 999, 1);
0

There are 0 best solutions below