ACF转发器查询不能使用%in field name

时间:2017-01-25 12:31:12

标签: wordpress advanced-custom-fields

我有ACF中继器字段,名为" courier_pricing _%_ price"并尝试查询这样的帖子

$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/

请帮助解决此问题,并提前多多感谢

1 个答案:

答案 0 :(得分:0)

找到了这个解决方案 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();
    }
}