How can I get a list of posts for a category with WP-API? olatechproApril 5, 20233 Views Should be easy, but didnt find it in the WP-API docs. Post Views: 3 Related postsGet rid of this Strict Standards warningWooCommerce: get_current_screen not working with multi languageCan’t login on WordPressForce HTTPS using .htaccess – stuck in redirect loopWordPress: Ajax not working to insert, query and result dataHow Can I pass an image file to wp_handle_upload?
Found it, somewhat [hidden][1] Using the category name: /posts?categories=1 Try this one. Log in to Reply
This question is a duplicate from this other question here from the forum http://example.com/wp-json/wp/v2/posts?categories=20,30 The above will return posts from category 20 OR category 30 I’ve tested with custom post types and it also works perfectly The response and credits go to “Manish Jung Thapa” Log in to Reply
For category name, two filters should be added like this: add_filter( "rest_post_query", function( $args, $request){ if ( isset( $request['category_name']) && !empty($request['category_name'] ) ) { $args['category_name'] = $request['category_name']; } return $args; }, 10, 2); add_filter( "rest_post_collection_params", function($query_params, $post_type){ $query_params[ 'category_name' ] = array( 'description' => __( 'Category name.' ), 'type' => 'string', 'readonly' => true, ); return $query_params; }, 10, 2); Log in to Reply
This code is working for me Add to your function.php function rest_filter_by_custom_taxonomy( $args, $request ) { if ( isset($request['category_slug']) ) { $category_slug = sanitize_text_field($request['category_slug']); $args['tax_query'] = [ [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => $category_slug, ] ]; } return $args; } add_filter('rest_post_query', 'rest_filter_by_custom_taxonomy', 10, 3); EX: /wp-json/wp/v2/posts?category_slug=news Log in to Reply
This example url worked for me… https://yourdomain.com/?rest_route=/wp/v2/posts&categories=99 Log in to Reply
Found it, somewhat [hidden][1]
Using the category name:
/posts?categories=1
Try this one.
This question is a duplicate from this other question here from the forum
The above will return posts from
category 20 OR category 30
I’ve tested with custom post types and it also works perfectly
The response and credits go to “Manish Jung Thapa”
For category name, two filters should be added like this:
This code is working for me
This example url worked for me… https://yourdomain.com/?rest_route=/wp/v2/posts&categories=99