我有一个自定义API端点来查询WPMU中另一个博客的数据,自定义端点会加载,我可以返回一些数据,但我需要的特定查询会返回0篇文章。
我有131篇这种自定义帖子类型的帖子,其中11篇与自定义分类法相关。
我可以用这个查询所有131篇帖子。
switch_to_blog(1);
$args = array(
\'post_type\' => \'podcast\',
\'posts_per_page\' => -1,
);
$query = new WP_Query( $args );
$posts = $query->posts;
restore_current_blog();
return $posts // returns 131 posts of custom post type
但是,当我应用自定义分类查询时,得到0个结果
switch_to_blog(1);
$args = array(
\'post_type\' => \'podcast\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'theme\',
\'field\' => \'slug\',
\'terms\' => \'my-slug\',
),
),
);
$query = new WP_Query( $args );
$posts = $query->posts;
restore_current_blog();
return $posts; // returns 0 posts
然后,我尝试获取自定义帖子类型的条款进行验证
invalid taxonomy
. 我的自定义分类法似乎没有在REST API中注册。
switch_to_blog(1)
$terms = get_terms( array(
\'taxonomy\' => \'themes\',
) );
restor_to_current_blog();
return $terms;
其输出为
code "invalid_taxonomy"
message "Invalid taxonomy."
data null
我还尝试在没有
switch_to_blog
函数并再次获取
invalid taxonomy
.
我还从函数中删除了自定义帖子类型的注册。php到mu插件,认为这可能是我的每个子博客使用不同主题的问题。但结果仍然是invalid taxonomy
.
这是我注册自定义分类法的方式
function my_custom_post_type_and_taxonomy() {
// Register Custom Taxonomy - theme for posts
$labels = array(
\'name\' => _x( \'Themes\', \'Taxonomy General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Theme\', \'Taxonomy Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Theme\', \'text_domain\' ),
\'all_items\' => __( \'All Themes\', \'text_domain\' ),
\'parent_item\' => __( \'Parent Theme\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Theme:\', \'text_domain\' ),
\'new_item_name\' => __( \'New Theme Name\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Theme\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Theme\', \'text_domain\' ),
\'update_item\' => __( \'Update Theme\', \'text_domain\' ),
\'view_item\' => __( \'View Item\', \'text_domain\' ),
\'separate_items_with_commas\' => __( \'Separate themes with commas\', \'text_domain\' ),
\'add_or_remove_items\' => __( \'Add or remove themes\', \'text_domain\' ),
\'choose_from_most_used\' => __( \'Choose from the most used themes\', \'text_domain\' ),
\'popular_items\' => __( \'Popular Items\', \'text_domain\' ),
\'search_items\' => __( \'Search themes\', \'text_domain\' ),
\'not_found\' => __( \'Not Found\', \'text_domain\' ),
\'no_terms\' => __( \'No items\', \'text_domain\' ),
\'items_list\' => __( \'Items list\', \'text_domain\' ),
\'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => true,
);
register_taxonomy( \'theme\', array( \'post\', \'podcast\' ), $args );
}
add_action( \'init\', \'my_custom_post_type_and_taxonomy\', 0 );