I had a question about Wordpress + rest api,
I use register_rest_route
for adding new end-point for my collection, and I also created a plugin named as 'article', and the type of this post type also is 'article'.
Now I wanna access all these post types in my new end-point with GET like this :
function my_awesome_func( $post_id ) {
$x = get_post_type_object( 'article' );
return $x[$post_id];
}
add_action( 'rest_api_init', function () {
register_rest_route( 'Articles/v2', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
I know you can't write $x[$post_id]
. I wrote it to say that I wanna access to specific article id. what should I do?
thanks.
I solve this problem finally, I post it, it may be useful for others.
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v2/Articles', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
function my_awesome_func( $data ) {
$args = array(
'post_type' => 'article',
'p' => $data['id'],
);
$query = new WP_Query( $args );
return $query->post;
}