url rewriting – Programmatically add a custom url route “/category/index.json” to return a collection of posts in json


I would like to serve a list of posts pertaining to a specific category in JSON format at this endpoint: /category/category-slug/index.json

I figured I could, as a start, point to the REST API for the json content.

How can I register this specific url ?
I tried using add_rewrite_rule but it only works on Apache whereas I’m on nginx so the function call has no effect.

add_action( 'init', 'my_redirects');
public function my_redirects(){
      // Redirects
      add_rewrite_rule('^/category/test/index\.json?','wp-json/wp/v2/posts?category=test', 'top');

pixeline 1 year 2022-11-15T06:22:25-05:00 0 Answers 0 views 0

Leave an answer