0
votes

I have ACF repeater field called "courier_pricing_%_price" and trying to query the posts like this

$args = array(
'posts_per_page' =>  -1,
'post_type'     => 'courier',
'meta_query'    => array(
    'relation'      => 'AND',
    array(
        'key'       => "courier_pricing_%_price",
        'compare'   => ">=",
        'value'     => '30',
    ),
  )
);

but it seems the "%" in the field name not working

the requested SQL is:

    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 

it seems the line with

wp_postmeta.meta_key = 'courier_pricing_%_price'

is the problem I change the "=" to "LIKE" and test the query in PHPMyAdmin and it working fine

I used the ACF damnation https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

please any help to fix this issue and many thanks in advance

1

1 Answers

0
votes

found this solution https://support.advancedcustomfields.com/forums/topic/meta-query-the-repeater-field/

and I used smaller way like this:

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();
    }
}