[英]Unset data from WordPress API (wp-json)
I can already unset (remove specifics from normal posts) in the json returned from the WordPress API.我已经可以在从 WordPress API 返回的 json 中取消设置(从普通帖子中删除细节)。 I actually use the following below from this example: https://css-tricks.com/using-the-wp-api-to-fetch-posts/
我实际上使用以下示例中的以下内容: https : //css-tricks.com/using-the-wp-api-to-fetch-posts/
What I am having trouble with and can't figure out, is how to change this so it unsets data from a Custom Post Type我遇到问题并且无法弄清楚的是如何更改它,以便它从自定义帖子类型中取消设置数据
Thoughts?想法?
function qod_remove_extra_data( $data, $post, $context ) {
// We only want to modify the 'view' context, for reading posts
if ( $context !== 'view' || is_wp_error( $data ) ) {
return $data;
}
// Here, we unset any data we don't want to see on the front end:
unset( $data['author'] );
unset( $data['status'] );
unset( $data['featured_image'] );
//etc etc
return $data;
}
add_filter( 'json_prepare_post', 'qod_remove_extra_data', 12, 3 );
custom post type example filter:自定义帖子类型示例过滤器:
function projectPost_remove_extra_data( $data, $post, $context ) {
if ( $context !== 'view' || is_wp_error( $data ) ) {
return $data;
}
// Here, we unset any data we don't want to see on the front end:
unset( $data['author'] );
return $data;
}
add_filter( 'json_prepare_project', 'projectPost_remove_extra_data', 12, 3 );
For wp-api v1.x, you need to extend WP_JSON_CustomPostType
.对于 wp-api v1.x,您需要扩展
WP_JSON_CustomPostType
。 There is an example in the pages file ( class-wp-json-pages.php
)页面文件中有一个示例(
class-wp-json-pages.php
)
<?php
/**
* Page post type handlers
*
* @package WordPress
* @subpackage JSON API
*/
/**
* Page post type handlers
*
* This class serves as a small addition on top of the basic post handlers to
* add small functionality on top of the existing API.
*
* In addition, this class serves as a sample implementation of building on top
* of the existing APIs for custom post types.
*
* @package WordPress
* @subpackage JSON API
*/
class WP_JSON_Pages extends WP_JSON_CustomPostType {
/**
* Base route
*
* @var string
*/
protected $base = '/pages';
/**
* Post type
*
* @var string
*/
protected $type = 'page';
/**
* Register the page-related routes
*
* @param array $routes Existing routes
* @return array Modified routes
*/
public function register_routes( $routes ) {
$routes = parent::register_routes( $routes );
$routes = parent::register_revision_routes( $routes );
$routes = parent::register_comment_routes( $routes );
// Add post-by-path routes
$routes[ $this->base . '/(?P<path>.+)'] = array(
array( array( $this, 'get_post_by_path' ), WP_JSON_Server::READABLE ),
array( array( $this, 'edit_post_by_path' ), WP_JSON_Server::EDITABLE | WP_JSON_Server::ACCEPT_JSON ),
array( array( $this, 'delete_post_by_path' ), WP_JSON_Server::DELETABLE ),
);
return $routes;
}
/**
* Retrieve a page by path name
*
* @param string $path
* @param string $context
*
* @return array|WP_Error
*/
public function get_post_by_path( $path, $context = 'view' ) {
$post = get_page_by_path( $path, ARRAY_A );
if ( empty( $post ) ) {
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
}
return $this->get_post( $post['ID'], $context );
}
/**
* Edit a page by path name
*
* @param $path
* @param $data
* @param array $_headers
*
* @return true|WP_Error
*/
public function edit_post_by_path( $path, $data, $_headers = array() ) {
$post = get_page_by_path( $path, ARRAY_A );
if ( empty( $post ) ) {
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
}
return $this->edit_post( $post['ID'], $data, $_headers );
}
/**
* Delete a page by path name
*
* @param $path
* @param bool $force
*
* @return true|WP_Error
*/
public function delete_post_by_path( $path, $force = false ) {
$post = get_page_by_path( $path, ARRAY_A );
if ( empty( $post ) ) {
return new WP_Error( 'json_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
}
return $this->delete_post( $post['ID'], $force );
}
/**
* Prepare post data
*
* @param array $post The unprepared post data
* @param string $context The context for the prepared post. (view|view-revision|edit|embed|single-parent)
* @return array The prepared post data
*/
protected function prepare_post( $post, $context = 'view' ) {
$_post = parent::prepare_post( $post, $context );
// Override entity meta keys with the correct links
$_post['meta']['links']['self'] = json_url( $this->base . '/' . get_page_uri( $post['ID'] ) );
if ( ! empty( $post['post_parent'] ) ) {
$_post['meta']['links']['up'] = json_url( $this->base . '/' . get_page_uri( (int) $post['post_parent'] ) );
}
return apply_filters( 'json_prepare_page', $_post, $post, $context );
}
}
Replace "Pages" with "MyCustomPostTypes" and page with "mycustomposttype".将“页面”替换为“MyCustomPostTypes”,将页面替换为“mycustomposttype”。 Just be careful not to rename internal WordPress code that also uses the term
page
请注意不要重命名也使用术语
page
内部 WordPress 代码
Note: probably best to add this as a plugin rather than change the JSON-WP-API plugin注意:最好将其添加为插件而不是更改 JSON-WP-API 插件
/**
* Plugin Name: MyCustom JSON App API
* Description: MyCustomPost handler for the JSON API
* Dependency: This plugin requires JSON-WP-API Plugin!!!!
* Author:
* Author URI:
* Version:
* Plugin URI:
*/
If possible, only the examples shown in internet is:如果可能,只有互联网上显示的示例是:
function qod_remove_extra_data ($ data, $ post, $ context) {
// We only want to modify the 'view' context, for reading posts
if ($ context! == 'view' || is_wp_error ($ data)) {
return $ data;
}
// Here, we unset any data we do not want to see on the front end:
unset ($data ['author']);
unset ($data ['status']);
// Continue unsetting whatever other fields you want return $ data;
}
add_filter ('json_prepare_post' 'qod remove extra_data', 12, 3);
and right is:正确的是:
qod_remove_extra_data function ($ data, $ post, $ context) {
// We only want to modify the 'view' context, for reading posts
if ($ context! == 'view' || is_wp_error ($ data)) {
unset ( $data->data ['excerpt']); //Example
unset ($data->data ['content']); //Example
unset ($data->data ['name field to remove'])
//or
unset ($data->data ['name field to remove'] ['name subfield if you only want to delete the sub-field of field' ])
return $data;
}
}
add_filter ('rest_prepare_post' 'qod_remove_extra_data', 12, 3);
IMPORTANT: Is: add_filter (' rest_prepare_post ' 'qod_remove_extra_data', 12, 3);重要:是:的add_filter( 'rest_prepare_post' 'qod_remove_extra_data',12,3);
Not: add_filter (' json_prepare_post ' 'qod remove extra_data', 12, 3);不是:add_filter(' json_prepare_post''qod remove extra_data', 12, 3); //WRONG
//错误的
If is Custom Post Type: add_filter (' rest_prepare_{$post_type} ' 'qod_remove_extra_data', 12, 3);如果是自定义帖子类型:add_filter(' rest_prepare_{$post_type} ''qod_remove_extra_data', 12, 3);
EXAMPLE: Name post type = product;示例:名称帖子类型 = 产品; add_filter (' rest_prepare_product ' 'qod_remove_extra_data', 12, 3);
add_filter(' rest_prepare_product''qod_remove_extra_data ', 12, 3);
With this code can remove the fields that you want the JSON.使用此代码可以删除您想要 JSON 的字段。 By using rest_prepare} _ {$ post_type decide that you eliminated every post_type fields, thus only affected the post_type you want and not all.
通过使用 rest_prepare} _ {$ post_type 决定你消除了每个 post_type 字段,从而只影响你想要的 post_type 而不是全部。
It should be no different to remove data from custom post types than from the built-in post types.从自定义帖子类型中删除数据应该与从内置帖子类型中删除数据没有什么不同。 Have you confirmed that your API call is actually returning your CPTs?
您是否确认您的 API 调用实际上返回了您的 CPT? First, you should look at the value of what is returned from:
http://yourwebsite.com/wp-json/posts/types
.首先,您应该查看从
http://yourwebsite.com/wp-json/posts/types
返回的值。 Assuming that your CPT type shows up there, you should be able to query for items of that type, eg product
, by calling: http://yourwebsite.com/wp-json/posts?type=product
.假设您的 CPT 类型显示在那里,您应该能够通过调用:http:
http://yourwebsite.com/wp-json/posts?type=product
?type= product
来查询该类型的项目,例如product
。
In other words, you should not change the name of the filter: you still want to tie into json_prepare_post
.换句话说,你不应该改变过滤器的名称:你还是想扎入
json_prepare_post
。 If you want to make your filter sensitive to post type and only remove certain fields if you have a CPT you could do something like:如果您想让您的过滤器对帖子类型敏感并且仅在您有 CPT 时删除某些字段,您可以执行以下操作:
function my_remove_extra_product_data( $data, $post, $context ) {
// make sure you've got the right custom post type
if ( 'product' !== $data[ 'type' ] ) {
return $data;
}
// now proceed as you saw in the other examples
if ( $context !== 'view' || is_wp_error( $data ) ) {
return $data;
}
// unset unwanted fields
unset( $data[ 'author' ] );
// finally, return the filtered data
return $data;
}
// make sure you use the SAME filter hook as for regular posts
add_filter( 'json_prepare_post', 'my_remove_extra_product_data', 12, 3 );
You can find more documentation in the WP API Getting Started Guide .您可以在WP API 入门指南 中找到更多文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.