Php WP-API在特定日期后获得帖子
我正在使用Php WP-API在特定日期后获得帖子,php,wordpress,date,advanced-custom-fields,wp-api,Php,Wordpress,Date,Advanced Custom Fields,Wp Api,我正在使用版本2.0-beta15和一个从WP\u REST\u Posts\u控制器继承的自定义post类型,但需要根据acf字段查询日期。哎呀 端点参数 /wp json/wp/v2/almanac_entry?per_page=3&filter[orderby]=acf_almanac_date&after=2016-12-23T00:00:00&filter[date_query[column]=acf_almanac_date 回应 响应返回三个项目,但只能返回两个,其中两个在列出的日
版本2.0-beta15
和一个从WP\u REST\u Posts\u控制器继承的自定义post类型,但需要根据acf字段查询日期。哎呀
端点参数
/wp json/wp/v2/almanac_entry?per_page=3&filter[orderby]=acf_almanac_date&after=2016-12-23T00:00:00&filter[date_query[column]=acf_almanac_date
回应
响应返回三个项目,但只能返回两个,其中两个在列出的日期之后,第三个在列出的日期之前。以下是acf\u almanac\u date
字段的三项值:
add_action( 'init', 'register_custom_post_types' );
function register_custom_post_types() {
global $wp_post_types;
$post_type_name = 'almanac_entry';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
add_action( 'rest_api_init', 'wp_rest_add_custom_fields' );
function wp_rest_add_custom_fields() {
register_rest_field('almanac_entry', 'acf_almanac_date', array (
'get_callback' => function($object, $field_name, $request) {
return get_post_meta( $object[ 'id' ], 'almanac_date', true ) . "T00:00:00";
},
'update_callback' => null,
'schema' => null,
));
}
非常感谢您的帮助
启示录1 我突然想到,可能param
filter[date_query[column]=acf_almanac_date
具有WP-API查询字段acf_almanac_date
,该字段是在WP_rest_add_custom_fields
函数中动态添加的
也许我需要扩展WP\u REST\u Posts\u控制器并覆盖
prepare\u items\u query
函数?如果为true,我如何将其与ACF字段ACF_almanac_date
关联?哎呀 WordPress REST API不允许通过开箱即用的post元数据进行查询,因为它认为这些元数据是私有的。要通过post元值启用查询,您需要:
WP\u query
的查询参数// Set the post type to modify.
$post_type = 'almanac_entry';
/**
* Register `almanac_date_before` and `almanac_date_after`
* as collection query params.
*
* Also support ordering by the `almanac_date` meta value.
*/
add_filter( "rest_{$post_type}_collection_params", function( $params ){
$params['almanac_date_before'] = array(
'description' => __( 'Limit response to posts published before a given ISO8601 compliant date.' ),
'type' => 'string',
'format' => 'date-time',
);
$params['almanac_date_after'] = array(
'description' => __( 'Limit response to posts published after a given ISO8601 compliant date.' ),
'type' => 'string',
'format' => 'date-time',
);
$params['orderby']['enum'][] = 'almanac_date';
return $params;
});
/**
* Transform almanac_date_before` and `almanac_date_after` into a meta query.
*/
add_filter( "rest_{$post_type}_query", function( $query_args, $request ){
if ( isset( $request['almanac_date_before'] ) ) {
if ( ! is_array( $query_args['meta_query'] ) ) {
$query_args['meta_query'] = array();
}
// We only want the 2016-11-23 from 2016-11-23T00:00:00
$bits = explode( 'T', $request['almanac_date_before'] );
$query_args['meta_query'][] = array(
'key' => 'almanac_date',
'value' => $bits[0],
'compare' => '<=',
'type' => 'DATE',
);
}
if ( isset( $request['almanac_date_after'] ) ) {
if ( ! is_array( $query_args['meta_query'] ) ) {
$query_args['meta_query'] = array();
}
// We only want the 2016-11-23 from 2016-11-23T00:00:00
$bits = explode( 'T', $request['almanac_date_after'] );
$query_args['meta_query'][] = array(
'key' => 'almanac_date',
'value' => $bits[0],
'compare' => '>=',
'type' => 'DATE',
);
}
return $query_args;
}, 10, 2 );
//设置要修改的帖子类型。
$post_type=‘历书条目’;
/**
*登记“历书日期”之前和“历书日期”之后`
*作为集合查询参数。
*
*还支持按“历书日期”元值排序。
*/
添加过滤器(“rest{$post\u type}}\u集合参数”,函数($params){
$params['almanac_date_']=数组(
“description”=>(限制对给定ISO8601兼容日期之前发布的帖子的响应)。”,
'类型'=>'字符串',
'格式'=>'日期时间',
);
$params['almanac\u date\u']=数组(
“description”=>(限制对给定ISO8601兼容日期后发布的帖子的响应。”),
'类型'=>'字符串',
'格式'=>'日期时间',
);
$params['orderby']['enum'][]='almanac_date';
返回$params;
});
/**
*将almanac_date_before`和almanac_date_after`转换为元查询。
*/
添加过滤器(“rest{$post\u type}}\u query”,函数($query\u args,$request){
如果(isset($request['almanac\u date\u before'])){
如果(!is_数组($query_args['meta_query'])){
$query_args['meta_query']=array();
}
//我们只想要2016-11-23T00:00:00的2016-11-23
$bits=explode('T',$request['almanac_date_before']);
$query\u args['meta\u query'][]=数组(
“关键”=>“年历日期”,
“值”=>$bits[0],
“比较”=>“=”,
'类型'=>'日期',
);
}
返回$query_args;
}, 10, 2 );
谢谢你的帮助,丹尼尔。不过还是有个简单的问题。如果我有以下5篇文章的年鉴日期为2016-12-31
,2016-12-24
,2016-12-17
,2016-12-10
,2016-12-03
,并且达到了终点:/wp-json/wp/v2/almanac\u条目?年鉴日期=2016-12-20T00:00
,我应该只有2篇文章2016-12-31
,2016-12-24
,但我得到了全部5分。我知道你参加了WP 4.7。也许我没有运行那个(我得看看),或者我错过了一个参数(我运行的是4.6.1版。也许我需要更新?假设这是问题所在,因为你明确调用了4.7,而不是我使用的是WP REST API版本2.0-beta15插件。WordPress 4.7将于12月6日发布。你可以使用Beta Tester插件更新到今天刚刚更新到4.7的候选发布版本,它的工作原理如下。)您概述了。再次感谢您的帮助!