Я поделюсь с вами тем, что я сделал.
Я создал новую конечную точку для своих пользовательских нужд, которая заканчивается как myapi/v1/posts/tag1/tag2
add_action('rest_api_init', function () {
register_rest_route( 'myapi/v1', 'posts/(?P<tag_1>[-\w]+)/(?P<tag_2>[-\w]+)',array(
'methods' => 'GET',
'callback' => 'get_posts_set'
));
});
Затем создал
function get_posts_set($request) {
$args = array(
'category_name' => $request['category_name'],
'tag_slug__and' => array( $request['tag_1'],$request['tag_2'] ),
);
$posts = get_posts($args);
if (empty($posts)) {
return new WP_Error( 'empty_category', 'there is no post in this category', array('status' => 404) );
}
// Following code is my processing of output. Customize it to suit your needs
$post_data = array();
$i = 0;
foreach( $posts as $post) {
$post_id = $post->ID;
$post_title = remove_html_comments($post->post_title);
$post_content = remove_html_comments($post->post_content);
$post_data[ $i ][ 'id' ] = $post_id;
$post_data[ $i ][ 'title' ] = $post_title;
$post_data[ $i ][ 'content' ] = $post_content;
$i++;
}
$response = new WP_REST_Response($post_data);
$response->set_status(200);
return $response;
}
function remove_html_comments($content = '') {
return preg_replace('/<!--(.|\s)*?-->/', '', $content);
}
Это вернет только PostID, заголовок и содержимое публикации с форматированием HTML для повторного использования директором. Надеюсь, что это решит ваш вопрос.