Skip to content

Instantly share code, notes, and snippets.

@ville6000
Last active April 18, 2016 07:12
Show Gist options
  • Save ville6000/0c090ee23567ce1737af to your computer and use it in GitHub Desktop.
Save ville6000/0c090ee23567ce1737af to your computer and use it in GitHub Desktop.
Add siblings field to WP REST API data
<?php
add_action( 'rest_api_init', 'slug_register_siglings' );
function slug_register_siglings() {
register_api_field( 'post',
'siblings',
array(
'get_callback' => 'slug_get_siglings',
'update_callback' => null,
'schema' => null,
)
);
}
function slug_get_siglings( $object, $field_name, $request ) {
$next_post = get_adjacent_post( false, '', false );
$prev_post = get_adjacent_post( false, '', true );
return array(
'next' => $next_post->ID,
'next_post_title' => $next_post->post_title,
'prev' => $prev_post->ID,
'prev_post_title' => $prev_post->post_title,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment