如何使用 WP_Query 获取对象的嵌套值?
How to use WP_Query to get nested values of an object?
我在网上搜索了很多,但都没有找到嵌套值
假设我有一个用户数组
[
{
id: 0,
billing: {
phone: "999999"
}
},
{
id: 1,
billing: {
phone: "777777"
}
}
]
我想使用 WP_Query 按 phone 号码过滤,可以吗?以及如何?
function get_customers(WP_REST_Request $request) {
$param = $request['phone'];
$args = array(
'post_type' => 'customer',
'posts_per_page' => 1,
'meta_query' => array(
'key' => 'billing.phone',
'value' => $param,
'compare' => '='
)
);
$customers = new WP_Query($args);
return $customers->posts;
}
add_action( 'rest_api_init', function() {
register_rest_route('rc/v1', 'customers/(?P<phone>\d+)', [
'methods' => 'GET',
'callback' => 'get_customers'
]);
});
首先,您的 meta_query
语法错误,它应该在另一个数组 check here 中,如果您的 post 元键是 'billing.phone'
那么下面的代码将起作用很好。
function get_customers( WP_REST_Request $request ) {
$param = $request['phone'];
$args = array(
'post_type' => 'customer',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'billing.phone',
'value' => $param,
'compare' => '='
)
)
);
$customers = new WP_Query($args);
return $customers->posts;
}
add_action( 'rest_api_init', function() {
register_rest_route('rc/v1', 'customers/(?P<phone>\d+)', [
'methods' => 'GET',
'callback' => 'get_customers'
]);
});
我在网上搜索了很多,但都没有找到嵌套值
假设我有一个用户数组
[
{
id: 0,
billing: {
phone: "999999"
}
},
{
id: 1,
billing: {
phone: "777777"
}
}
]
我想使用 WP_Query 按 phone 号码过滤,可以吗?以及如何?
function get_customers(WP_REST_Request $request) {
$param = $request['phone'];
$args = array(
'post_type' => 'customer',
'posts_per_page' => 1,
'meta_query' => array(
'key' => 'billing.phone',
'value' => $param,
'compare' => '='
)
);
$customers = new WP_Query($args);
return $customers->posts;
}
add_action( 'rest_api_init', function() {
register_rest_route('rc/v1', 'customers/(?P<phone>\d+)', [
'methods' => 'GET',
'callback' => 'get_customers'
]);
});
首先,您的 meta_query
语法错误,它应该在另一个数组 check here 中,如果您的 post 元键是 'billing.phone'
那么下面的代码将起作用很好。
function get_customers( WP_REST_Request $request ) {
$param = $request['phone'];
$args = array(
'post_type' => 'customer',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'billing.phone',
'value' => $param,
'compare' => '='
)
)
);
$customers = new WP_Query($args);
return $customers->posts;
}
add_action( 'rest_api_init', function() {
register_rest_route('rc/v1', 'customers/(?P<phone>\d+)', [
'methods' => 'GET',
'callback' => 'get_customers'
]);
});