WordPress Rest API - Get all posts based on post_meta on custom endpoint
So I can't seem to figure out how to get ALL posts based on a specific post meta, so I wanted to reach out and see if someone might be able to assist.
What I am trying to achieve:
- I have
website A, which has posts that I can access using/wp-json/wp/v2/postswhich outputs ALL posts on the website. - Then I have
website Bwhich I want to accesswebsite Aposts, but I want to pull in only specific posts based on post_meta over the Rest API.
What I have added on website A:
if (!function_exists('post_meta_request_params')) {
function post_meta_request_params($args, $request): array
{
$args += [
'meta_key' = $request['meta_key'],
'meta_value' = $request['meta_value'],
'meta_query' = $request['meta_query'],
];
return $args;
}
add_filter('rest_post_query', 'post_meta_request_params', 99, 2);
}
Where I can access the Rest API using /wp-json/wp/v2/posts?meta_key=author_idmeta_value=104, which works on website A, but the problem is, when I attempt to use it on website B, it doesn't work using wp_remote_request:
$response = wp_remote_request(add_query_arg( [
'page' = 1,
'per_page' = 3,
'post_status' = 'publish',
], 'https://www.websiteA.com/wp-json/wp/v2/posts?meta_key=author_idmeta_value=104'));
Could I create a custom endpoint with just posts with post_meta author_id and meta_value of 104, so that I can access that endpoint on website B?
If so, does anyone have an example of a register_rest_route with feeding posts with specific meta, so that I can access for example /wp-json/wp/v2/custom_posts on website B?