0

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/posts which outputs ALL posts on the website.
  • Then I have website B which I want to access website A posts, 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_id&meta_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_id&meta_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?

1
  • Are you sure wp_remote_request( 'https://www.websiteA.com/wp-json/wp/v2/posts?meta_key=author_id&meta_value=104' ) doesn't work? What's in the response body? Did you see any errors?
    – Sally CJ
    May 6 at 5:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.