我使用Version 2.0-beta15
的自定义帖子类型继承自WP_REST_Posts_Controller,但需要根据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_almanac_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
我突然想到,参数filter[date_query[column]]=acf_almanac_date
可能会对acf_almanac_date
函数中动态添加的字段wp_rest_add_custom_fields
进行WP-API查询。
也许我需要扩展WP_REST_Posts_Controller并覆盖prepare_items_query
函数?如果为true,我该如何将其与ACF字段acf_almanac_date
相关联? Oy vey!
答案 0 :(得分:1)
WordPress REST API不允许通过发布元数据来查询元数据,因为它认为它们是私有的。要通过post meta值启用查询,您需要:
<?php
setlocale(LC_ALL, "pt_BR");
header('Content-type: text/html; charset=iso-8859-1');
?>
<html>
<head>
<script src="js/jquery/jquery-1.6.4.min.js"></script>
<script src="js/jquery/jquery.maskedinput.js"></script>
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet">
<!-- O problema esta nesta linha abaiso, com ela carregando funciona a data mas não funciona a mascara do telefone -->
<!-- My problem is in the next line, if I remove it .mask fom jQuery works fine, but I lost datapicker -->
<script type="text/javascript" src="scripts/lib/jquery.js"></script>
<!-- In oposite, if I leave this line datatimepicker works as well but, .mask stop to work -->
<!-- se eu comentar a linha ocorre o contrario, funciona a mascar mas a data não abre a janela para selecionar o dia -->
</head>
<form method='POST' id='teste' name='teste' action='teste.php'>
Telefone:<input type='text' class='telefone' name='fone'><BR><BR>
Data Nascimento:
<div class='controls input-append date form_date' data-date='' data-date- format='dd/mm/yyyy' data-link-field='dtp_input1' data-link-format='yyyy-mm-dd'>
<input size='16' type='text' value='22/11/2016' id='data_nascimento' name='data_nascimento' readonly >
<span class='add-on'><i class='icon-remove'></i></span>
<span class='add-on'><i class='icon-th'></i></span>
</div>
</form>
<script type="text/javascript" src="./bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="./js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript" src="./js/locales/bootstrap-datetimepicker.pt-BR.js" charset="UTF-8"></script>
<script type="text/javascript">
$('.form_date').datetimepicker({
language: 'pt-BR',
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
forceParse: 0
});
</script>
<script type="text/javascript">
jQuery("input.telefone")
.mask("(99) 9999-9999?9")
.focusout(function (event) {
var target, phone, element;
target = (event.currentTarget) ? event.currentTarget : event.srcElement;
phone = target.value.replace(/\D/g, '');
element = $(target);
element.unmask();
if(phone.length > 10) {
element.mask("(99) 99999-999?9");
} else {
element.mask("(99) 9999-9999?9");
}
});
</script>
</html>
的查询参数。这里有一些适用于WordPress 4.7的代码:
WP_Query