How to filter by meta
-
I would like to be able to retrieve a Custom Post Type by a particular meta-key’s value.
I have exposed Custom post type and meta-key via the plugin and it does show up in the response. However, when I try either of the following, I am getting ALL posts (for this custom Post Type) instead of the one I need.
https://test.com/wp-json/wp/v2/customtype?filter[metaKeyName]=metaKeyValue
https://test.com/wp-json/wp/v2/customtype?filter[meta_key]=metaKeyName&filter[meta_value]=metaKeyValue
I also attempted to add the following code to allow searching by meta_keys:
add_filter( 'rest_query_vars', 'my_allow_meta_query' ); function my_allow_meta_query( $valid_vars ) { $valid_vars = array_merge( $valid_vars, array( 'meta_key', 'meta_value' ) ); return $valid_vars; }
I notice that an extra meta node exists on the response. However, the value is null (in the JSON body) and the each meta key/value node looks as if it has been copied to the top level “fields”. So, I would expect that your plugin is basically converting the custom fields to “regular” fields in the response. Following this logic, I would have expected the first rest URL format to work.
Please help! What am I doing wrong?
- The topic ‘How to filter by meta’ is closed to new replies.