How do you get Posts by multiple meta_keys and meta_values with the Rest API V2?
-
Im trying to get my Posts by multiple meta_keys and meta_values. How do i accomplish this ? The URL should be looking like this:
“/posts?meta_key=Example&meta_value=Example2&meta_key=Example3&meta_value=Example4”
I tried to find a solution for this quite a while now, but couldn’t find anything the most things where outdated. I hope someone can help me with this.- This topic was modified 4 years ago by Yui. Reason: moved to "developing..." section
-
I don’t think you can with the default
posts
route/endpoint. Looking at WP_REST_Posts_Controller::get_items() source code, there is no provision for qualifying queries by meta data. You likely need to either develop a custom route for the purpose, or have the calling app sort it out from the returned data of a generic posts request.In any case be sure to expose your meta keys to the API with
register_meta()
or nothing will work no matter what you do.I got an custom Enpoint wich is looking like this: “wordpress/wp-json/wp/v2/newtype”. It’s not the default route for Posts. I’m trying to get Posts by multiple meta_keys and meta_values. What i already can do is to get a Post by One meta_key and meta_value but not multiple. How can i do it that it takes multiple Meta Data in the request.
Or how can i get all Posts with their Meta Data of a custom type Post. I alreay tried doing it with “new WP_Query”. This is working im getting my Posts but without any Meta Data. How can i get the Posts with their Meta Data ?
With WP_Query you use the “meta_query” argument to get posts by complex meta data criteria. You’ll see that it requires nested arrays to correctly use. You’ll need to get creative in passing such data as API query strings. One way to pass simple indexed arrays is like this:
custom/route?key[]=foo&key[]=bar&value[]=123&value[]=456
Your endpoint would need to reassemble that into a proper meta_query argument. Another possibility is have your calling app serialize and escape a properly formed array and pass that as the route’s query string.So i got this for enableling a Meta request per API.
if (!function_exists('post_meta_rest_api_request')) : function post_meta_rest_api_request($argu, $request) { $argu += array( 'meta_key' => $request['meta_key'], 'meta_value' => $request['meta_value'], 'meta_query' => $request['meta_query'], ); return $argu; } add_filter('rest_custom_query', 'post_meta_rest_api_request', 99, 2); endif;
How am i gonna do a meta_query over the API. Could you give any example ?
What I suggested earlier were conceptual. I’ve not actually implemented anything, hence I’ve no example, sorry. The serializing the array concept that passes it as an URL query string “meta_query” value has more merit I think. The API handler then unserializes and uses it to form the WP_Query.
I found a solution for my Problem. What i did is quite simple. I just setted up a new Query Params.
if (!function_exists('post_meta_rest_api_request')) : function post_meta_rest_api_request($argu, $request) { $argu += array( 'meta_key' => $request['meta_key'], 'meta_value' => $request['meta_value'], 'meta_query' => $request['meta_query'] == 1 ? array( array( "key" => "key1", "value" => "value1" ), array( "key" => "key2", "value" => "value2" ) ) : $request['meta_query'] ); return $argu; } add_filter('rest_custom_query', 'post_meta_rest_api_request', 99, 2); endif;
So if you now make an API call like:
wordpress/wp-json/wp/v2/customType?meta_query=1
the API Request will take in your custom Query Params. Otherwise it will just take in the normal Meta Query Request.
For references look here: WordPress Rest-API Requests- This reply was modified 4 years ago by nkudev.
- The topic ‘How do you get Posts by multiple meta_keys and meta_values with the Rest API V2?’ is closed to new replies.