最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom taxonomy - get_term_by works but get_terms doesn't?

programmeradmin2浏览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?

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?

Share Improve this question asked Dec 26, 2015 at 9:34 Austin BiggsAustin Biggs 3843 silver badges19 bronze badges 1
  • I think this link will help you. It has similar question. Please check answer: http://wordpress.stackexchange/questions/189035/get-all-terms-inside-a-specific-taxonomy-in-a-multisite – Prasad Nevase Commented Dec 26, 2015 at 9:49
Add a comment  | 

1 Answer 1

Reset to default 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/ticket/35231#ticket

发布评论

评论列表(0)

  1. 暂无评论