因此,我正在使用WordPress REST API,我想引入all the posts from the last 2 weeks
个,大约14天.
在WordPress REST API参数中,我可能可以使用before
和after
参数(https://developer.wordpress.org/rest-api/reference/posts/#example-request),但我不确定如何采用这种方法.
正在使用$response = wp_remote_get
调用WordPress REST API端点.
以下是我目前获取所有帖子的完整方法:
public function get_posts_via_rest_api(): array
{
$page = get_option('global_posts_page');
if (!$page) {
$page = 1;
}
try {
$response = wp_remote_get(
'https://example.com/wp-json/wp/v2/posts?page=' . $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 [];
}
}