admin管理员组

文章数量:1312800

I have a father blog / main blog inside a multisite that has a specific taxonomy with 20+ terms inside it. i just need a list of all the terms inside that taxonomy...

i dont mind quering using WPDB but just dont know to how to write it correctly or in any other way i tried use: "switch_to_blog" but that doesnt work when quering terms

Any suggestion?
i tried the: Multisite Global Terms plugin (doesnt work)...

EDIT (response to question in comment)
this is an example of switch_to_blog i tried:

function multisite_profession_select(){

        switch_to_blog(1);

        $taxonomies = array('rsitecat');
        $args       = array('hide_empty' => false);
        $terms      = get_terms($taxonomies, $args );

        echo '<pre>';
        print_r($terms);
        echo '</pre>';

        restore_current_blog();

    }

The reponse i get:

WP_ERROR OBJECT
(
    [ERRORS] => ARRAY
        (
            [INVALID_TAXONOMY] => ARRAY
                (
                    [0] => INVALID TAXONOMY
                )

        )

    [ERROR_DATA] => ARRAY
        (
        )

)

I have a father blog / main blog inside a multisite that has a specific taxonomy with 20+ terms inside it. i just need a list of all the terms inside that taxonomy...

i dont mind quering using WPDB but just dont know to how to write it correctly or in any other way i tried use: "switch_to_blog" but that doesnt work when quering terms

Any suggestion?
i tried the: Multisite Global Terms plugin (doesnt work)...

EDIT (response to question in comment)
this is an example of switch_to_blog i tried:

function multisite_profession_select(){

        switch_to_blog(1);

        $taxonomies = array('rsitecat');
        $args       = array('hide_empty' => false);
        $terms      = get_terms($taxonomies, $args );

        echo '<pre>';
        print_r($terms);
        echo '</pre>';

        restore_current_blog();

    }

The reponse i get:

WP_ERROR OBJECT
(
    [ERRORS] => ARRAY
        (
            [INVALID_TAXONOMY] => ARRAY
                (
                    [0] => INVALID TAXONOMY
                )

        )

    [ERROR_DATA] => ARRAY
        (
        )

)
Share Improve this question edited May 21, 2015 at 22:34 Sagive asked May 21, 2015 at 20:39 SagiveSagive 2,7822 gold badges35 silver badges54 bronze badges 7
  • What exactly had you tried with switch_to_blog()? In general case it should work... – Rarst Commented May 21, 2015 at 21:09
  • i have edited my question to include a more readbale reponse. – Sagive Commented May 21, 2015 at 22:36
  • Hmhm... You sure the name is correct, no typo in it or anything? What if you try with native taxonomy like category? – Rarst Commented May 22, 2015 at 8:40
  • will try now Rarst (and thanks for trying to help) – Sagive Commented May 22, 2015 at 16:54
  • (a while later - too much work) - works for category - doesnt for taxonomy... the tax name is the correct one. also tried other taxonomies. is this an option in wordpress? if so.. is there a way to go around the issuse? – Sagive Commented Jun 8, 2015 at 13:51
 |  Show 2 more comments

3 Answers 3

Reset to default 7 +50

No way to do that in a "word press" way...

function multisite_profession_select(){
    switch_to_blog(1);
    $taxonomies = array('rsitecat');

    $check_later = array();
    global $wp_taxonomies;
    foreach($taxonomies as $taxonomy){
        if (isset($wp_taxonomies[$taxonomy])){
            $check_later[$taxonomy] = false;
        } else {
            $wp_taxonomies[$taxonomy] = true;
            $check_later[$taxonomy] = true;
        }
    }

    $args       = array('hide_empty' => false);
    $terms      = get_terms($taxonomies, $args );

    echo '<pre>';
    print_r($terms);
    echo '</pre>';


    if (isset($check_later))
        foreach($check_later as $taxonomy => $unset)
            if ($unset == true)
                unset($wp_taxonomies[$taxonomy]);

    restore_current_blog();
}

WP will check if Taxonomy exists, but on multisite blog, its only switching $table_prefix but not actually switching to blog. In this fixed code - it should trick WP to think that taxonomy registred and retrive a term from a mu blog tables. after request - you killing fake taxonomy ghosts.

P/S/

I didn't test code.

If you have problems getting data from another blog, polyfixes like temporarily registering taxonomies, etc. are a real pita in the long term.

You can't really rely on that workarounds, since such workarounds mimic internal wp infrastructure that changes over time.

Best practice imho would be to register a rest api endpoint (https://developer.wordpress/rest-api/extending-the-rest-api/adding-custom-endpoints/) or an xmlrpc endpoint (https://codex.wordpress/XML-RPC_Extending). The endpoint fetches all data you need and returns it as json. So you always have a clean local context of the blog queried and don't have to struggle with missing infrastructure.

Would be something like this in the source blog:

add_action( 
    'rest_api_init', 
    function () {
        register_rest_route('your-namespace', '/get-terms/(?P<taxonomy>\d+)', array(
            'methods' => 'GET',
            'callback' => function($data) {
                $taxonomy_slug = $data['taxonomy'];
                return get_terms($taxonomy_slug, array('hide_empty' => false));
            }
        ));
    }
);

And something like that in the requesting blog:

$api_request = 'http://the.domain.of.your.blog/wp-json/your-namespace/get-terms/slug-of-taxonomy';
$api_response = wp_remote_get($api_request);
$taxonomy_terms = json_decode(wp_remote_retrieve_body($api_response), true);

Hope, that helps and best regards from Salzburg!

This is another way to solve, with wpdb object:

function multisite_profession_select(){
    global $wpdb;

    switch_to_blog(1);

    $taxonomies = array('rsitecat');
    $taxonomies = implode( "','", $taxonomies );

    $terms = $wpdb->get_results( "
        SELECT t.*
        FROM
            {$wpdb->prefix}term_taxonomy AS tt
        INNER JOIN
            {$wpdb->prefix}terms AS t
            ON t.term_id = tt.term_id
        WHERE
            tt.taxonomy IN ( '{$taxonomies}' )
    " );

    echo '<pre>';
    print_r($terms);
    echo '</pre>';

    restore_current_blog();
}

本文标签: Get all terms inside a specific taxonomy in a multisite