diff --git a/README.md b/README.md index 52f7d61..1eed574 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ Gets a collection of posts. Accepts the following parameters: - per_page (int) - tag id (int) - yoast (boolean - setting to false omits `yoast` from being returned) +- meta_key (string) +- meta_value (string) +- meta_compare (string) It returns a JSON response with the following: - ACF fields, if applicable @@ -169,6 +172,9 @@ Gets a collection of posts from a custom post type. Accepts the following parame - page (int) - per_page (int) - yoast (boolean - setting to false omits `yoast` from being returned) +- meta_key (string) +- meta_value (string) +- meta_compare (string) Returns the following JSON response: diff --git a/includes/create_cpt_endpoints.php b/includes/create_cpt_endpoints.php index 801eae0..b5a183a 100644 --- a/includes/create_cpt_endpoints.php +++ b/includes/create_cpt_endpoints.php @@ -37,6 +37,9 @@ function bre_build_cpt_endpoints() { $orderby = $request['orderby']?: null; $order = $request['order']?: null; $exclude = $request['exclude']?: null; + $meta_key = $request['meta_key']?: null; + $meta_value = $request['meta_value']?: null; + $meta_compare = $request['meta_compare']?: null; // WP_Query arguments $args = array( @@ -46,7 +49,14 @@ function bre_build_cpt_endpoints() { 'paged' => $page, 'post__not_in' => array($exclude), 'order' => $order?:'DESC', - 'orderby' => $orderby?:'date' + 'orderby' => $orderby?:'date', + 'meta_query' => array( + array( + 'key' => $meta_key, + 'value' => $meta_value, + 'compare' => $meta_compare + ) + ) ); // The Query @@ -244,6 +254,30 @@ function bre_build_cpt_endpoints() { }, 'sanitize_callback' => 'absint' ), + 'meta_key' => array( + 'description' => 'Query the collection by meta_key.', + 'type' => 'string', + 'validate_callback' => function($param, $request, $key) { + return is_string( $param ); + }, + 'sanitize_callback' => 'sanitize_text_field', + ), + 'meta_value' => array( + 'description' => 'Query the collection by meta_value.', + 'type' => 'string', + 'validate_callback' => function($param, $request, $key) { + return is_string( $param ); + }, + 'sanitize_callback' => 'sanitize_text_field', + ), + 'meta_compare' => array( + 'description' => 'Set the meta comparison for querying the collection by meta value.', + 'type' => 'string', + 'validate_callback' => function($param, $request, $key) { + return is_string( $param ); + }, + 'sanitize_callback' => 'sanitize_text_field', + ), ), ) ); diff --git a/includes/get_posts.php b/includes/get_posts.php index 23ac5dd..24b78a3 100644 --- a/includes/get_posts.php +++ b/includes/get_posts.php @@ -27,6 +27,9 @@ function bre_get_posts( WP_REST_Request $request ) { $order = $request['order']?: null; $exclude = $request['exclude']?: null; $author = $request['author']?: ''; + $meta_key = $request['meta_key']?: null; + $meta_value = $request['meta_value']?: null; + $meta_compare = $request['meta_compare']?: null; // WP_Query arguments $args = array( @@ -39,7 +42,14 @@ function bre_get_posts( WP_REST_Request $request ) { 'order' => $order?:'DESC', 'orderby' => $orderby?:'date', 'post__not_in' => array($exclude), - 'author_name' => $author + 'author_name' => $author, + 'meta_query' => array( + array( + 'key' => $meta_key, + 'value' => $meta_value, + 'compare' => $meta_compare + ) + ) ); // The Query @@ -320,6 +330,30 @@ function bre_get_posts( WP_REST_Request $request ) { }, 'sanitize_callback' => 'sanitize_text_field', ), + 'meta_key' => array( + 'description' => 'Query the collection by meta_key.', + 'type' => 'string', + 'validate_callback' => function($param, $request, $key) { + return is_string( $param ); + }, + 'sanitize_callback' => 'sanitize_text_field', + ), + 'meta_value' => array( + 'description' => 'Query the collection by meta_value.', + 'type' => 'string', + 'validate_callback' => function($param, $request, $key) { + return is_string( $param ); + }, + 'sanitize_callback' => 'sanitize_text_field', + ), + 'meta_compare' => array( + 'description' => 'Set the meta comparison for querying the collection by meta value.', + 'type' => 'string', + 'validate_callback' => function($param, $request, $key) { + return is_string( $param ); + }, + 'sanitize_callback' => 'sanitize_text_field', + ), ), ) ); } );