Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down
36 changes: 35 additions & 1 deletion includes/create_cpt_endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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',
),
),
) );

Expand Down
36 changes: 35 additions & 1 deletion includes/get_posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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',
),
),
) );
} );