0

I want to use Wordpress REST API to get the posts that are inside a custom taxonomy created by the theme.

The taxonomy is called "my_portfolio" but if I use this I have this response:

{"code":"rest_no_route","message":"Nessun percorso fornisce una corrispondenza tra l'URL ed il metodo richiesto.","data":{"status":404}}

Instead if I use "my_portfolio_category" I have 4 categories that are called: "photo", "video", "design", "web".

I want to retrieve the posts inside the category "photos" but the only thing I managed to get is the category info.

The API I used is the one down below where 921 is the ID of the category "photo", using the slug doesn't work.

https://www.mywebsite.com/wp-json/wp/v2/my_portfolio_category/921

Is there a way to get the posts?

2 Answers 2

1

You need to first register rest route for your custom taxonomy i.e. my_portfolio

add_filter( 'register_taxonomy_args', 'my_portfolio_taxonomy', 10, 2 );
 
function my_portfolio_taxonomy( $args, $taxonomy_name ) {
 
    if ( 'my_portfolio' === $taxonomy_name ) {
        $args['show_in_rest'] = true;
 
        // Optionally customize the rest_base or rest_controller_class
        $args['rest_base']             = 'my-portfolio';
        $args['rest_controller_class'] = 'WP_REST_Terms_Controller';
    }
 
    return $args;
}

After that you can access url like : http://yourdomain.in/wp-json/wp/v2/posts?my-portfolio=28

Please note 28 is custom taxonomy id. you need to add id of photos in that case

1

You need to have 'show_in_rest' => true as an argument when you register your taxonomy with register_taxonomy. Then you'll get a route by WordPress. No need to create your own route.

The route will be: .../wp/v2/my_portfolio

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.