1

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 );

1 Answer 1

1

Using this REST API End Point https://example.com/wp-json/wp/v2/taxonomies I found I had 3 Taxonomies available post_tag, category and series.

After seeing series, a custom post type, I knew I had something missing in my code.

I finally found an article Working with Custom Post Types in WP-API v2 which describes my issue. Apparently in API V2 there are some new arguments added in for the REST API. The specific argument I needed was 'show_in_rest' => true, and by adding it into my arguments array resolved my issue.

$args = array(
    'labels'       => $labels,
    ...
    'show_in_rest' => true,
    ...
);
register_taxonomy( 'theme', array( 'post', 'resource', 'podcast' ), $args );

More information about this can be found in the codex https://developer.wordpress.org/reference/functions/register_taxonomy/

3
  • Note that the REST API has been v2 since 2015, and merged into WordPress core since 2016, so make sure you're looking at up-to-date documentation by starting with the official docs: developer.wordpress.org/rest-api And ignoring any docs that refer to the API as a plugin. Commented Mar 10, 2021 at 7:21
  • 1
    Also, note that switch_to_blog() does not change the active plugins or themes, so if a post type is only registered on one blog, it will be not be registered by running switch_to_blog() to that blog. Commented Mar 10, 2021 at 7:22
  • I was completely aware of the need to use current docs, I registered this custom taxonomy at least 6 years ago or more when I built the site. It was a recent need to create a new sub blog in the WPMU network and using the API to obtain data. It was the disconnect in time that made this frustrating. That last comment is great, I had no idea the switch_to_blog() would not switch active theme or plugins, but it did cross my mind and that's why I moved the registration to a mu-plugin. Thanks for the feedback and info.
    – stoi2m1
    Commented Mar 11, 2021 at 4:28

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.