I need to get the adjecent posts of a custom post type "projects". The custom post type works in general and I get the a projects list and single projects via GET request. In addition want to add the adjecent projects for simple navigation on the single project page in the response.
I am using
'rest_base' => 'projects'
The below code works for standard posts and I am trying to figure out how to adapt it so I can access the custom post in the same way. How do I access the custom post type correctly? Any help appreciated.
function chrest_adjacent_posts( $response, $post, $request ) {
global $post;
$next = get_adjacent_post( false, '', false );
$previous = get_adjacent_post( false, '', true );
$response->data['next'] = ( is_a( $next, 'WP_Post') ) ? array( "id" => $next->ID, "slug" => $next->post_name ) : null;
$response->data['previous'] = ( is_a( $previous, 'WP_Post') ) ? array( "id" => $previous->ID, "slug" => $previous->post_name ) : null;
return $response;
}
add_filter( 'rest_prepare_post', 'chrest_adjacent_posts', 10, 3 );
I need to get the adjecent posts of a custom post type "projects". The custom post type works in general and I get the a projects list and single projects via GET request. In addition want to add the adjecent projects for simple navigation on the single project page in the response.
I am using
'rest_base' => 'projects'
The below code works for standard posts and I am trying to figure out how to adapt it so I can access the custom post in the same way. How do I access the custom post type correctly? Any help appreciated.
function chrest_adjacent_posts( $response, $post, $request ) {
global $post;
$next = get_adjacent_post( false, '', false );
$previous = get_adjacent_post( false, '', true );
$response->data['next'] = ( is_a( $next, 'WP_Post') ) ? array( "id" => $next->ID, "slug" => $next->post_name ) : null;
$response->data['previous'] = ( is_a( $previous, 'WP_Post') ) ? array( "id" => $previous->ID, "slug" => $previous->post_name ) : null;
return $response;
}
add_filter( 'rest_prepare_post', 'chrest_adjacent_posts', 10, 3 );
Share
Improve this question
asked Jun 14, 2019 at 12:39
Carsten HCarsten H
1277 bronze badges
2
|
1 Answer
Reset to default 1That filter is actually dynamic, so you'll just need to append your post type:
add_filter( 'rest_prepare_projects', 'chrest_adjacent_posts', 10, 3 );
https://github/WP-API/WP-API/blob/develop/lib/endpoints/class-wp-rest-posts-controller.php#L1355
Hope that helps!!!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745402319a4626157.html
after
andbefore
properties to get posts made before and after the current post. – Jacob Peattie Commented Jun 14, 2019 at 13:04