0

I currently have a Multisite with 2 sites.

From the secondary site get_term_by() works to retrieve terms from the primary site, but get_terms() does not.

I'm using switch_to_blog(1); but I get object(WP_Error)#1316 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data"]=> array(0) { } } when I run get_terms()

Is there a way around this?

1

1 Answer 1

1

I figured out the answer, which seems to be an issue with WordPress.

Using get_terms() to get terms from another site WORKS, BUT A TAXONOMY BY THE SAME NAME MUST BE PRESENT ON THE SUBSITE YOU'RE WORKING FROM.

Below is my code (working):

switch_to_blog(1);

$taxonomy = array( 'taxonomy_name' );

$args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false, 
    'exclude'           => array(), 
    'exclude_tree'      => array(), 
    'include'           => array(),
    'number'            => '', 
    'fields'            => 'all', 
); 

$terms = get_terms( $taxonomy, $args );

var_dump($terms);

restore_current_blog();

UPDATE: I've submitted a WP bug to Trac https://core.trac.wordpress.org/ticket/35231#ticket

5
  • Every site in a Multisite installation is a separate site with its own set of taxonomies. This sounds like the expected behaviour.
    – Pat J
    Commented Jan 2, 2016 at 5:20
  • How would this be the expected behavior? There are functions (functions I'm using) that allow a site to access any other site. Get_terms_by is working, but get_terms is not. Commented Jan 2, 2016 at 6:33
  • Can you please edit your question to add in the code that wasn't working? That might help figure out exactly what was going on.
    – Pat J
    Commented Jan 2, 2016 at 15:30
  • Pat, please reread my post. This is the code that is and isn't working. It only works if a taxonomy of the same name exists on the subsite. Which is not the intended functionality since I am switching sites in my code. Commented Jan 6, 2016 at 20:44
  • This is expected behaviour, using switch_to_blog won't unload the plugins and theme then load in that sites plugins/theme, it just switches the database tables and one or two globals
    – Tom J Nowell
    Commented Aug 28, 2020 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.