One way that comes to my mind is to define your own custom routes for the API, allowing it to send results based on the route or use the same route with different GET params.
OR based on the way you’re going with, you could probably send some GET params to the url and do things based on them by getting the params from $request
and have conditions based on the params.
Something like:
add_filter('rest_post_query', function($args, $request) {
$queryParams = $request->get_query_params();
$want_meta = isset( $queryParams['want_the_meta_ones'] ) AND $queryParams['want_the_meta_ones'] == 'yes';
if( $want_meta ){
$args += [
'meta_key' => $request['meta_key'],
'meta_value' => $request['meta_value'],
'meta_query' => $request['meta_query'],
];
} else {
$args += [
'meta_key' => $request['meta_key'],
'meta_compare' => 'NOT EXISTS',
];
}
return $args;
}, 99, 2);
Mixed with:
$response = wp_remote_get(
'https://example-1.com/wp-json/v2/posts?page=" . $page_number . "&meta_key=original_id&want_the_meta_ones=yes'
);
or:
$response = wp_remote_get(
'https://example-1.com/wp-json/v2/posts?page=" . $page_number . "&meta_key=original_id&want_the_meta_ones=no'
);
Update 1:
if we want the original query to be untouched in case there is no specefic url param, this is how we do it:
add_filter('rest_post_query', function($args, $request) {
$queryParams = $request->get_query_params();
$meta_action = isset( $queryParams['meta_action'] ) AND $queryParams['meta_action'];
if( $meta_action ){
if( $meta_action == 'include_meta' ){
$args += [
'meta_key' => $request['meta_key'],
'meta_value' => $request['meta_value'],
'meta_query' => $request['meta_query'],
];
} elseif( $meta_action == 'exclude_meta' ) {
$args += [
'meta_key' => $request['meta_key'],
'meta_compare' => 'NOT EXISTS',
];
}
}
return $args;
}, 99, 2);
this way if the param meta_action
is not set to either include_meta
or exclude_meta
, the main query is untouched. I think include_meta
and exclude_meta
what they do is kind of self-explanatory. I also added some gaps in code and did it in the most basic way so you could get the idea and expand it based on your needs.
the call would look like this:
$response = wp_remote_get(
'https://example-1.com/wp-json/v2/posts?page=" . $page_number . "&meta_key=original_id&meta_action=include_meta'
);