How to add a filter to a custom post type to get adjacent custom posts via the REST API

I need to get the adjecent posts of a custom post type "projects". The custom post type works in general and I

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
  • The proper way to do this with REST, I believe, would be to make a request to the normal endpoint, just using the after and before properties to get posts made before and after the current post. – Jacob Peattie Commented Jun 14, 2019 at 13:04
  • Thanks Jakob, as far as I can remember these properies are related to posts before and after a certain date, I will look into that. – Carsten H Commented Jun 14, 2019 at 14:06
Add a comment  | 

1 Answer 1

Reset to default 1

That 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信