diff --git a/classes/PodsRESTFields.php b/classes/PodsRESTFields.php index c2aeab5dff..851a524ff6 100644 --- a/classes/PodsRESTFields.php +++ b/classes/PodsRESTFields.php @@ -40,6 +40,7 @@ public function __construct( $pod ) { if ( $this->pod ) { add_action( 'rest_api_init', [ $this, 'add_fields' ] ); + add_filter( 'rest_' . $this->pod->get_name() . '_query', [ $this, 'query_fields' ], 10, 2 ); } } @@ -88,6 +89,37 @@ private function set_pod( $pod ) { $this->pod = $pod; } + /** + * @since 3.0.9 + * + * @param \WP_REST_Request $request ArrayAccess + * @param array $args + * + * @return array + */ + public function query_fields( $args, $request ) { + $fields = $this->pod->get_fields(); + $meta_query = []; + + foreach ( $fields as $field ) { + $name = $field->get_name(); + if ( isset( $request[ $name ] ) ) { + $value = $request[ $name ]; + $meta_query[] = [ + 'key' => $name, + 'compare' => is_array( $value ) ? 'IN' : '=', + 'value' => $value, + ]; + } + } + + if ( $meta_query ) { + $args['meta_query']['pods'] = $meta_query; + } + + return $args; + } + /** * Add fields, based on options to REST read/write requests *