So I’m working with the WordPress REST API and I wish to pull in all of the posts from the final 2 weeks
, so roughly 14 days.
Within the WordPress REST API arguments, they’ve a earlier than
and after
argument (https://developer.wordpress.org/rest-api/reference/posts/#example-request) that I would be capable to make the most of, however I am uncertain take this strategy.
The WordPress REST API endpoint is being known as utilizing $response = wp_remote_get
.
Right here is my accomplished technique to drag in ALL submit for now:
public perform get_posts_via_rest_api(): array
{
$web page = get_option('global_posts_page');
if (!$web page) {
$web page = 1;
}
attempt {
$response = wp_remote_get(
'https://instance.com/wp-json/wp/v2/posts?web page=" . $web page
);
if ((!is_wp_error($response)) && (200 === wp_remote_retrieve_response_code($response))) {
$response_body = json_decode(
$response["body'],
false,
512,
JSON_THROW_ON_ERROR
);
return empty($response_body) ? [] : $response_body;
}
} catch (Exception $e) {
error_log(
print_r(
'Error: ' . $e,
true
)
);
return [];
}
}