ACF 转发器查询不适用于文件名中的 %
ACF repeater query not working with % in filed name
我有一个名为 "courier_pricing_%_price" 的 ACF 转发器字段并尝试查询这样的帖子
$args = array(
'posts_per_page' => -1,
'post_type' => 'courier',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => "courier_pricing_%_price",
'compare' => ">=",
'value' => '30',
),
)
);
但字段名称中的“%”似乎不起作用
请求的SQL是:
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND (
( wp_postmeta.meta_key = 'courier_pricing_%_price' AND wp_postmeta.meta_value = '30' )
) AND wp_posts.post_type = 'courier' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_posts.post_date DESC
似乎是
wp_postmeta.meta_key = 'courier_pricing_%_price'
是我将“=”更改为 "LIKE" 并在 PHPMyAdmin 中测试查询并且它工作正常的问题
我用了ACF诅咒
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
请帮忙解决这个问题,非常感谢
找到这个解决方案
https://support.advancedcustomfields.com/forums/topic/meta-query-the-repeater-field/
我使用了这样更小的方法:
function whereCourier( $where )
{
$where = str_replace("meta_key = 'courier_pricing_%_price'", "meta_key LIKE 'courier_pricing_%_price'", $where);
return $where;
}
function searchCourier()
{
$meta_query[] = [
'relation' => 'AND',
[
'key' => "courier_pricing_%_price",
'compare' => ">=",
'value' => '30',
],
];
$args = [
'posts_per_page' => -1,
'post_type' => 'courier',
'meta_query' => $meta_query
];
add_filter('posts_where', 'whereCourier');
$query = new WP_Query($args);
remove_filter('posts_where', 'whereCourier');
return $query;
}
$query = searchCourier();
if($query->have_posts()){
while ($query->have_posts()) {
$query->the_post();
echo get_the_ID();
}
}
我有一个名为 "courier_pricing_%_price" 的 ACF 转发器字段并尝试查询这样的帖子
$args = array(
'posts_per_page' => -1,
'post_type' => 'courier',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => "courier_pricing_%_price",
'compare' => ">=",
'value' => '30',
),
)
);
但字段名称中的“%”似乎不起作用
请求的SQL是:
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND (
( wp_postmeta.meta_key = 'courier_pricing_%_price' AND wp_postmeta.meta_value = '30' )
) AND wp_posts.post_type = 'courier' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order, wp_posts.post_date DESC
似乎是
wp_postmeta.meta_key = 'courier_pricing_%_price'
是我将“=”更改为 "LIKE" 并在 PHPMyAdmin 中测试查询并且它工作正常的问题
我用了ACF诅咒 https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
请帮忙解决这个问题,非常感谢
找到这个解决方案 https://support.advancedcustomfields.com/forums/topic/meta-query-the-repeater-field/
我使用了这样更小的方法:
function whereCourier( $where )
{
$where = str_replace("meta_key = 'courier_pricing_%_price'", "meta_key LIKE 'courier_pricing_%_price'", $where);
return $where;
}
function searchCourier()
{
$meta_query[] = [
'relation' => 'AND',
[
'key' => "courier_pricing_%_price",
'compare' => ">=",
'value' => '30',
],
];
$args = [
'posts_per_page' => -1,
'post_type' => 'courier',
'meta_query' => $meta_query
];
add_filter('posts_where', 'whereCourier');
$query = new WP_Query($args);
remove_filter('posts_where', 'whereCourier');
return $query;
}
$query = searchCourier();
if($query->have_posts()){
while ($query->have_posts()) {
$query->the_post();
echo get_the_ID();
}
}