Last active
November 25, 2024 07:21
-
-
Save maheshwaghmare/0bbe5eabceed24aa76ef1eabe684a748 to your computer and use it in GitHub Desktop.
Search post by post meta with Rest API. Read the article https://maheshwaghmare.com/search-post-by-post-meta-with-rest-api/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add meta fields support in rest API for post type `Post` | |
* | |
* This function will allow custom parameters within API request URL. Add post meta support for post type `Post`. | |
* | |
* > How to use? | |
* http://mysite.com/wp-json/wp/v2/posts?meta_key=<my_meta_key>&meta_value=<my_meta_value> | |
* | |
* > E.g. Get posts which post meta `already-visited` value is `true`. | |
* | |
* Request like: http://mysite.com/wp-json/wp/v2/post?meta_key=already-visited&meta_value=true | |
* | |
* @since 1.0.0 | |
* | |
* @link https://codex.wordpress.org/Class_Reference/WP_Query | |
* | |
* @see Wp-includes/Rest-api/Endpoints/Class-wp-rest-posts-controller.php | |
* | |
* @param array $args Contains by default pre written params. | |
* @param array $request Contains params values passed through URL request. | |
* @return array $args New array with added custom params and its values. | |
*/ | |
if( ! function_exists( 'post_meta_request_params' ) ) : | |
function post_meta_request_params( $args, $request ) | |
{ | |
$args += array( | |
'meta_key' => $request['meta_key'], | |
'meta_value' => $request['meta_value'], | |
'meta_query' => $request['meta_query'], | |
); | |
return $args; | |
} | |
add_filter( 'rest_post_query', 'post_meta_request_params', 99, 2 ); | |
// add_filter( 'rest_page_query', 'post_meta_request_params', 99, 2 ); // Add support for `page` | |
// add_filter( 'rest_my-custom-post_query', 'post_meta_request_params', 99, 2 ); // Add support for `my-custom-post` | |
endif; |
How could I add multiple custom fields to it? Let's say I have two custom fields one named country and the other city and I want to find posts that match the custom fields country=spain and city=madrid?
@wtfmejt Here is an example used by a coworker, using meta_query
to use two specific parameters: https://gist.github.com/cruzmayra/075b49ade44a19d22013bc8764606d80
Good gist file, it works for me.
What do you think of the proposal from @ravimallya, @cruzmayra?
I'd love to see another fork of that gist, but avoiding the explosion of complexity that has been added to support a range of multiple months.
It works tough with month names instead of a number name? About the range of years, I don't know why that should be a useful use case.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How could I add multiple custom fields to it? Let's say I have two custom fields one named country and the other city and I want to find posts that match the custom fields country=spain and city=madrid?