【问题标题】:ACF repeater query not working with % in filed nameACF 转发器查询不适用于字段名称中的 %
【发布时间】:2017-01-25 12:31:11
【问题描述】:

我有一个名为“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/

请帮助解决这个问题,非常感谢提前

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 2016-06-17
      • 2017-02-22
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      相关资源
      最近更新 更多