Skip to content

Instantly share code, notes, and snippets.

@klihelp
Created February 1, 2015 19:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klihelp/fb4f292a1ec20e97efab to your computer and use it in GitHub Desktop.
Save klihelp/fb4f292a1ec20e97efab to your computer and use it in GitHub Desktop.
WP-API modify post response fields
<?php
/**
* WP-API modify post response fields
*
*/
/**
* Posts per page
*/
function kli_wp_api_query_post_per_page( $query ){
if ( defined( 'JSON_REQUEST' ) && TRUE == JSON_REQUEST ){
$query->set( 'posts_per_page', -1);
}
}
add_action( 'pre_get_posts', 'kli_wp_api_query_post_per_page' );
/**
* Modify Post Response - Remove Attachments
* @param object $data
* @return object
*/
function kli_wp_api_json_prepare_post( $data ){
/**
* Remove featured_image field from response
*
*/
remove_filter( 'json_prepare_post', array( $wp_json_media, 'add_thumbnail_data' ), 10 , 3);
return $data;
}
add_action( 'json_prepare_post', 'kli_wp_api_json_prepare_post', 5);
/**
* Prepare post fields - Remove some fields
* @param object $data
* @return object
*/
function kli_wp_api_prepare_post( $data ){
// var_dump($data);
// exit;
// Remove fields
// !!todo - check core feature - last checked jan2015
$fields = explode(',', 'status,author,guid,parent,date,format,'
.'_links,'
.'comment_status,ping_status,sticky,date_gmt,featured_image');
// var_dump($fields);
// var_dump(each($fields));
// exit;
foreach ($fields as $value) {
if ( isset($data[$value]) ) {
unset($data[$value]);
}
}
return $data;
}
add_action( 'json_prepare_post', 'kli_wp_api_prepare_post', 100);
/**
* Prepare post fields - Remove attachments fields
* @param object $data
* @return object
*/
function kli_wp_api_prepare_attachment( $data ){
// Remove fields
// @TODO: check core feature - updated jan2015
$fields = explode(',', 'type,menu_order,'
.'_links,title,link,'
.'content,modified,slug,modified_gmt,source,is_image,');
// attachment_meta fields
$attachment_meta_fields = explode(',', 'image_meta,'
.'width,height,file'
);
if (isset($fields) && count($fields)) {
foreach ($fields as $value) {
if ( isset($data[$value]) ) {
unset($data[$value]);
}
}
}
if (isset($attachment_meta_fields) && count($attachment_meta_fields) && isset($data['attachment_meta'])) {
foreach ($attachment_meta_fields as $value) {
if ( isset($data['attachment_meta'][$value]) ) {
unset($data['attachment_meta'][$value]);
}
}
}
return $data;
}
add_action( 'json_prepare_attachment', 'kli_wp_api_prepare_attachment', 100);
@fobar21
Copy link

fobar21 commented Apr 14, 2015

Perfect, but have a one problem.

API alway return meta data (links, self, author, coollection, replies and version-history).
Can i filter this?

@luetkemj
Copy link

API always returns meta data (links, self, author, collection, replies and version-history).
Can i filter this?

Need to do this as well.

@aishan-shrestha
Copy link

i ve use this but nothing seems working.
i use the code but when i view page by wp-json/wp/v2/posts it showing them all. Is there anything i missing still?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment