I have a Custom API End Point to query data from another blog in WPMU, the custom end point loads and I am able to return some data, but the specific query I need returns 0 posts.
I have 131 posts in this custom post type and 11 of which are associated with the custom taxonomy.
I can query all 131 posts by using this.
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
However, when I apply the custom taxonomy query I get 0 results
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
I then tried to get terms of the custom post type to verify and I get invalid taxonomy
. It appears my custom taxonomy isn't being registered within the REST API.
switch_to_blog(1)
$terms = get_terms( array(
'taxonomy' => 'themes',
) );
restor_to_current_blog();
return $terms;
The output of which is
code "invalid_taxonomy"
message "Invalid taxonomy."
data null
I also tried to query the custom taxonomy without the switch_to_blog
function and again got invalid taxonomy
.
I also moved the registration of my custom post type from the functions.php to a mu-plugin thinking it could be an issue of using different themes for each of my sub blogs. But the results were still invalid taxonomy
.
This is how I register my custom 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 );