admin管理员组文章数量:1129003
I am aware that I can use the endpoint /wp-json/wp/v2/posts/{id}
to fetch a post.
I am building an implementation of WordPress that relies completely on the JSON API and its endpoints. It is an Angular SPA implementation, which means my hooks need to fire when a request is made via the API.
As such, if I want to record views for a post, I wanted to use 'posts_selection'
. My first question is will this fire if the selection is made via /wp-json/wp/v2/posts/{id}
My second question is is there another hook or something that will also fire during API query for a post but which will pass the {id}
of the post to my function?
Because when I do this:
function check_assembled_query( $query ) {
var_dump( $query );
}
add_action( 'posts_selection', 'check_assembled_query' );
$query
doesn't have the information I want. I would like the following workflow:
- Request a post using
/wp-json/wp/v2/posts/{id}
from AngularJS. - Have the backend recognize this request and iterate a meta field of the post's views using
update_post_meta()
which requires a postid
. - Return the post requested to my JavaScript function with all the post's information, including aforementioned meta-info view-count.
EDIT: I take it post_selection
doesn't fire for REST API because when I do this:
function check_assembled_query( $query ) {
var_dump( $query );
die();
}
add_action( 'posts_selection', 'check_assembled_query' );
I only get the dump on regular requests rather than requests to the JSON API.
I am aware that I can use the endpoint /wp-json/wp/v2/posts/{id}
to fetch a post.
I am building an implementation of WordPress that relies completely on the JSON API and its endpoints. It is an Angular SPA implementation, which means my hooks need to fire when a request is made via the API.
As such, if I want to record views for a post, I wanted to use 'posts_selection'
. My first question is will this fire if the selection is made via /wp-json/wp/v2/posts/{id}
My second question is is there another hook or something that will also fire during API query for a post but which will pass the {id}
of the post to my function?
Because when I do this:
function check_assembled_query( $query ) {
var_dump( $query );
}
add_action( 'posts_selection', 'check_assembled_query' );
$query
doesn't have the information I want. I would like the following workflow:
- Request a post using
/wp-json/wp/v2/posts/{id}
from AngularJS. - Have the backend recognize this request and iterate a meta field of the post's views using
update_post_meta()
which requires a postid
. - Return the post requested to my JavaScript function with all the post's information, including aforementioned meta-info view-count.
EDIT: I take it post_selection
doesn't fire for REST API because when I do this:
function check_assembled_query( $query ) {
var_dump( $query );
die();
}
add_action( 'posts_selection', 'check_assembled_query' );
I only get the dump on regular requests rather than requests to the JSON API.
Share Improve this question edited Nov 18, 2017 at 23:11 Summer Developer asked Nov 18, 2017 at 23:03 Summer DeveloperSummer Developer 4158 silver badges21 bronze badges 1- It is better to use it developer.wordpress.org/reference/functions/register_rest_field So you get full control over your field in response of the REST server – StAlKeRxXl Commented Jul 9, 2021 at 12:08
1 Answer
Reset to default 5My original answer was all wrong, so it's been removed in it's entirety.
Neither the posts_selection
nor wp
hook are fired during the REST API request.
The hook you need is rest_pre_echo_response
. This hook passes three parameters:
- The response data to send to the client
- The server instance.
- The request used to generate the response.
Since you need the post ID, you can do something like:
add_filter( 'rest_pre_echo_response', function( $response, $object, $request ) {
//* Get the post ID
$post_id = $response[ 'id' ];
//* Make sure of the post_type
if( 'post' !== $response[ 'post' ] ) return $response;
//* Do something with the post ID
//* Return the new response
return $response;
}, 10, 3 );
本文标签: Rest API Hook When Post Is Requested
版权声明:本文标题:Rest API Hook When Post Is Requested 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736734616a1950184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论