get_term_by works but get_terms doesn't?

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?

Topic switch-to-blog terms custom-taxonomy multisite Wordpress

Category Web


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.