WordPress REST API – Meta Query and Post Status
-
Hello,
I want to use WordPress REST API in order to run some meta queries for my custom post type ‚products‘.
I’ve added the filter parameter in my functions.php which works fine.
Example URL: http://test/blog/wp-json/wp/v2/products?filter%5Bmeta_query%5D%5B0%5D%5Bkey%5D=price&filter%5Bmeta_query%5D%5B0%5D%5Bvalue%5D=40/** * Add the filter parameter * * @param array $args The query arguments. * @param WP_REST_Request $request Full details about the request. * @return array $args. **/ function wp_rest_filter_add_filter_param( $args, $request ) { // Bail out if no filter parameter is set. if ( empty( $request['filter'] ) || ! is_array( $request['filter'] ) ) { return $args; } $filter = $request['filter']; if ( isset( $filter['posts_per_page'] ) && ( (int) $filter['posts_per_page'] >= 1 && (int) $filter['posts_per_page'] <= 100 ) ) { $args['posts_per_page'] = $filter['posts_per_page']; } global $wp; $vars = apply_filters( 'rest_query_vars', $wp->public_query_vars ); function allow_meta_query( $valid_vars ) { $valid_vars = array_merge( $valid_vars, array( 'meta_query', 'meta_key', 'meta_value', 'meta_compare' ) ); return $valid_vars; } $vars = allow_meta_query( $vars ); foreach ( $vars as $var ) { if ( isset( $filter[ $var ] ) ) { $args[ $var ] = $filter[ $var ]; } } return $args; } add_filter( 'rest_products_query', 'wp_rest_filter_add_filter_param', 10, 2 );
I’m also using custom post statuses for my post type, for example ‚archive‘. In the result list only products with status ‚publish‘ are regarded.
When I add a status parameter to URL e.g. http://test/blog/wp-json/wp/v2/products?filter%5Bmeta_query%5D%5B0%5D%5Bkey%5D=price&filter%5Bmeta_query%5D%5B0%5D%5Bvalue%5D=40&status=archive I’m getting an error message as well ‚Invalid parameter: status‘.How can I ‚allow‘ the post statuses to my query? Can somebody help me, please?
Best regards
- Das Thema „WordPress REST API – Meta Query and Post Status“ ist für neue Antworten geschlossen.