0

I am looking at a custom taxonomy endpoint and there are custom post types for this taxonomy but they don't show in the endpoint. Is there something I need to add to this endpoint or something to the registering of the custom post type/taxonomy?

http://example.com/wp-json/wp/v2/experiences/7

This just gives me fields id, count, description, link, name, slug, taxonomy, parent, meta, links. But no actual custom post type data.

1 Answer 1

1

From your comments:

I just wanted to find out if there was a core endpoint for listing custom taxonomies with associated post type records.

And as I said in my original answer: (slightly modified, though)

The endpoint in question is a taxonomy term endpoint which returns the data (e.g. description, name and slug) for a specific term, but the response does not include posts in that term because posts have their own endpoints (e.g. /wp/v2/your_post_type?experiences=7 to get posts of the your_post_type type in the custom experiences taxonomy for term with the ID 7), but only if the post type is enabled in the REST API

So no, there is no core endpoint that works out-of-the-box like regular term archive pages like one at example.com/category/uncategorized, example.com/?cat=1 or example.com/experiences/foo-bar (where experiences is a custom taxonomy slug), which by default displays posts from all or any post types associated with the taxonomy of the requested term.

I was hoping I could get this from one endpoint

Perhaps a custom end point would be better in this case

Yes, that's an easy way as opposed to making multiple API requests for the different post types associated with your taxonomy, and it also saves you from possibly having to do complex JS coding for dealing with paging.

But actually, you could just make use of the Search endpoint (/wp/v2/search) along with the rest_post_search_query filter to modify the search query args which will be passed to WP_Query.

So that means, just like you can control the main WordPress query via the pre_get_posts hook, you can also control the Search endpoint query via the above filter.

So you can add taxonomy queries, meta queries, etc.

And here's an example that you can try, where you would make a request to /wp/v2/search?type=post&<taxonomy slug>=<term ID or slug>, e.g. /wp/v2/search?type=post&experiences=7 or /wp/v2/search?type=post&experiences=term-slug.

Notes:

  • The type=post above must always be that and it's not a post type! Instead, it means you're searching for posts.

  • In the response, the actual post type (slug) is in the property named subtype.

add_filter( 'rest_post_search_query', 'my_rest_post_search_query', 10, 2 );
function my_rest_post_search_query( $query_args, $request ) {
    $tax_query  = array();
    $post_types = array();

    $tax_objects = get_taxonomies( array(
        'publicly_queryable' => true,
    ), 'objects' );

    foreach ( $tax_objects as $taxonomy => $tax_object ) {
        if ( ! empty( $request[ $taxonomy ] ) ) {
            $tax_query[] = array(
                'taxonomy' => $taxonomy,
                'terms'    => $request[ $taxonomy ],
                'field'    => is_numeric( $request[ $taxonomy ] ) ? 'term_id' : 'slug',
            );

            $post_types = array_merge( $post_types, (array) $tax_object->object_type );
        }
    }

    if ( ! empty( $tax_query ) ) {
        $query_args['tax_query'] = $tax_query;
        $query_args['post_type'] = $post_types;
    }

    return $query_args;
}
9
  • Thanks so much for your answer Sally. Will read through it in more detail shortly but going back to your first line. What I essentially would like to do is going to a custom taxonomy endpoint and see all the custom post type entries for that taxonomy there. So, if the custom taxonomy was CARS, when I hit that endpoint I would like to see the custom post type entries I created there eg: AUDI, BMW, CHEV, TOYOTA, HYUNDAI and so on. PS. I do have the show_in_rest set to true already. Commented Jul 14, 2021 at 15:34
  • That isn't implemented in core, but as an alternative, you could use the /wp/v2/taxonomies/experiences endpoint to get the post types in the taxonomy, and then use the /wp/v2/types endpoint to get the post type data?
    – Sally CJ
    Commented Jul 14, 2021 at 15:50
  • Sorry, to further elaborate I basically want to use javascript to get data from the api and render it on the page. If I have 2 tabs (category names) I want to list the post type records in each tab. I was hoping I could get this from one endpoint so I could basically loop through the categories and it's associated posts. In each iteration I would display the category tab and list the post type data. Perhaps a custom end point would be better in this case? I just wanted to find out if there was a core endpoint for listing custom taxonomies with associated post type records. Commented Jul 14, 2021 at 15:54
  • "Perhaps a custom end point would be better in this case?" - yes, maybe. But on what page does your JS calls the REST API? A custom plugin options page? Using jQuery?
    – Sally CJ
    Commented Jul 14, 2021 at 16:02
  • 1
    Hi there, apologies for the delayed response. Thanks so much, that works really well! :) Commented Jul 16, 2021 at 6:31

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.