如何通过WordPress REST API将元字段设为搜索关键词?
Got it, let's figure out how to make this work. You’ve already got custom meta fields set up for both posts and your events custom post type, and you can POST to the REST API—now you want to let users search using those meta fields via GET requests. The old tutorials you found are probably relying on outdated hooks, so here’s a modern approach that works with WordPress 5.8 and later:
First, make sure when you registered your meta fields (with register_post_meta() or register_meta()), you set show_in_rest to true. This is non-negotiable for using them in REST searches. Example:
// For standard posts register_post_meta( 'post', 'event_location', array( 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'auth_callback' => function() { return current_user_can( 'edit_posts' ); } ) ); // Repeat for your 'events' CPT register_post_meta( 'events', 'event_location', array( 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'auth_callback' => function() { return current_user_can( 'edit_events' ); // Match your CPT's edit capability } ) );
We’ll create a reusable class to handle adding a custom search parameter (like meta_search) and modifying the REST query to check your meta fields. This plays nice with multiple post types and uses the latest REST API hooks:
class Meta_Search_REST_Handler { protected $post_types = array( 'post', 'events' ); public function __construct() { // Hook into REST initialization add_action( 'rest_api_init', array( $this, 'register_meta_search_param' ) ); // Add query filters for each post type foreach ( $this->post_types as $type ) { add_filter( "rest_{$type}_query", array( $this, 'filter_rest_query_for_meta_search' ), 10, 2 ); } } // Register the custom 'meta_search' parameter for endpoints public function register_meta_search_param() { foreach ( $this->post_types as $type ) { add_filter( "rest_{$type}_collection_params", function( $params ) { $params['meta_search'] = array( 'description' => 'Search posts/events by matching a term in custom meta fields', 'type' => 'string', ); return $params; } ); } } // Modify the query to include meta field search public function filter_rest_query_for_meta_search( $args, $request ) { $search_term = $request->get_param( 'meta_search' ); if ( ! empty( $search_term ) ) { // List the meta keys you want to search through $target_meta_keys = array( 'event_location', 'event_organizer' ); // Replace with your keys // Build an OR meta query to check all target keys $meta_query = array( 'relation' => 'OR' ); foreach ( $target_meta_keys as $key ) { $meta_query[] = array( 'key' => $key, 'value' => $search_term, 'compare' => 'LIKE', // Use '=' for exact matches instead ); } // Merge with existing meta queries if any $args['meta_query'] = isset( $args['meta_query'] ) ? array_merge( $args['meta_query'], $meta_query ) : $meta_query; } return $args; } } // Initialize the handler new Meta_Search_REST_Handler();
- Swap out
event_locationandevent_organizerwith your actual meta keys. - Change the
comparevalue to=if you need exact matches instead of partial ones. - Add a capability check in
filter_rest_query_for_meta_searchif you want to restrict this search to specific user roles.
Make a GET request to your REST endpoints with the meta_search parameter:
- For posts:
https://your-site.com/wp-json/wp/v2/post?meta_search=New+York - For events:
https://your-site.com/wp-json/wp/v2/events?meta_search=New+York
This will return all posts/events where any of your target meta fields contain the search term.
Quick Notes
- We’re using
rest_{post_type}_queryfilters instead of the oldpre_get_postshook—this avoids conflicts with other REST API functionality and is the recommended approach for modern WordPress. - The class structure keeps your code clean, especially since you’re handling multiple post types.
内容的提问来源于stack exchange,提问作者Jeda Dragon

