Support

Account

Home Forums Backend Issues (wp-admin) Multi_select taxonomy from other blog

Helping

Multi_select taxonomy from other blog

  • Hi,
    I have a multi-site wordpress Blog with 8 sub-sites.
    In the parent-theme I create a taxonomy called/slugged “Vip” (more than 10000 entries). The menu tax is only visible in the main-blog.
    I want that this taxonomy will be “global” for every sub-sites.
    If, in a child-blog, I use a “checkbox_select” all works fine (it’s so slowly, more than 10000 entries in a list).
    Instead if I use a multi_select from ACF PRO (with ajax calls) it doesn’t work well:
    – ajax call fail if I use search (probably because I’m not in the correct blog!??!)
    – if I don’t use search, ajax call return me a list of “vip” terms, and when I save the post, terms selected are saved correctly in a postmeta, but, when the page reload, the multi_select option is empty (I think because I’m not in the correct blog!??!)

    I think I need to do a switch_to_blog() and restore_current_blog() but when?
    Someone can help me?

    Sorry for the bad english 🙂

  • Hi @mikiamomik

    I’m afraid this is hard to achieve. I think you can use the acf/fields/taxonomy/query hook to mark if a query is requested by ACF like this:

    function my_taxonomy_query( $args, $field, $post_id ) {
        // add custom args to be checked by get_terms hook
        $args['test'] = 'testing';
        
        // return
        return $args;
    }
    add_filter('acf/fields/taxonomy/query', 'my_taxonomy_query', 10, 3);

    After that, you can modify the returned terms using the “get_terms” hook like this:

    function example_callback_taxonomy( $terms, $taxonomies, $args ) {
        if(isset($args['test'])){ //check if it called from acf/fields/taxonomy/query
            remove_action('get_terms', 'example_callback_taxonomy', 20);
            
            // modify the args if you want
            $args['order'] = 'ASC';
            
            //get it from other blog
            switch_to_blog($blog_id)
            $terms = get_terms( 'category', $args );
            restore_current_blog()
            
            add_filter('get_terms', 'example_callback_taxonomy', 20, 3);
            
            return $terms;
            
        }
    }
    add_filter( 'get_terms', 'example_callback_taxonomy', 20, 3 );

    The value wasn’t showing up because ACF is checking if your current blog has the taxonomy or not. You need to modify the select 2 field using jQuery and the acf/input/admin_footer.

    I hope this makes sense.

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

The topic ‘Multi_select taxonomy from other blog’ is closed to new replies.