custom post types - Meta Query "IN" doesn't work with ACF checkbox filter

I try to integrate to my CPT archive page some ACF filters. I start with this tutorial : It works when I use it with A

I try to integrate to my CPT archive page some ACF filters. I start with this tutorial : / It works when I use it with ACF radio field (like the example in the tutorial). Now, I try to make it work with ACF checkbox field. When I select one filter, I have no results...

This is my code :

$GLOBALS['my_query_filters'] = array(
'field_5cb6ef1f75209'  => 'alcool',
);


// action
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);

function my_pre_get_posts( $query ) {

// bail early if is in admin
if( is_admin() ) return;


// bail early if not main query
// - allows custom code / plugins to continue working
//if( !$query->is_main_query() ) return;

// get meta query
$meta_query = $query->get('meta_query');


// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {

    // continue if not found in url
    if( empty($_GET[ $name ]) ) {

        continue;

    }

    // get the value for this filter
    // eg: ,sydney
    $value = explode(',', $_GET[ $name ]);

    // append meta query
    $meta_query = array(
        array(
            'key'     => $name,
            'value'       => $value,
            'compare'  => 'IN',
        )
    );

}

// update meta query
$query->set('meta_query', $meta_query);
return;

}

My field 'alcool' is a checkbox. When I replace it with a radio field, it's okay. But I have to use checkboxes.

I try several solutions and I work around arrays and strings, but nothing work...

Thank's !

I try to integrate to my CPT archive page some ACF filters. I start with this tutorial : https://www.advancedcustomfields/resources/creating-wp-archive-custom-field-filter/ It works when I use it with ACF radio field (like the example in the tutorial). Now, I try to make it work with ACF checkbox field. When I select one filter, I have no results...

This is my code :

$GLOBALS['my_query_filters'] = array(
'field_5cb6ef1f75209'  => 'alcool',
);


// action
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);

function my_pre_get_posts( $query ) {

// bail early if is in admin
if( is_admin() ) return;


// bail early if not main query
// - allows custom code / plugins to continue working
//if( !$query->is_main_query() ) return;

// get meta query
$meta_query = $query->get('meta_query');


// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {

    // continue if not found in url
    if( empty($_GET[ $name ]) ) {

        continue;

    }

    // get the value for this filter
    // eg: http://www.website/events?city=melbourne,sydney
    $value = explode(',', $_GET[ $name ]);

    // append meta query
    $meta_query = array(
        array(
            'key'     => $name,
            'value'       => $value,
            'compare'  => 'IN',
        )
    );

}

// update meta query
$query->set('meta_query', $meta_query);
return;

}

My field 'alcool' is a checkbox. When I replace it with a radio field, it's okay. But I have to use checkboxes.

I try several solutions and I work around arrays and strings, but nothing work...

Thank's !

Share Improve this question asked Apr 30, 2019 at 15:07 Nicolas LorandNicolas Lorand 237 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Checkbox field is stored as serialized array, therefore you can not use the IN operator and array with the values you are looking for.

To get posts with checked "melbourne", change meta_query to:

$meta_query = array(
    array(
        'key'      => $name,
        'value'    => '"melbourne"',
        'compare'  => 'LIKE',
    )
);

To get posts with melbourne or sydney:

$meta_query = array(
    'relation' => 'OR',
    [
        'key'      => $name,
        'value'    => '"melbourne"',
        'compare'  => 'LIKE',
    ],
    [
        'key'      => $name,
        'value'    => '"sydney"',
        'compare'  => 'LIKE',
    ],
);

Think about changing the solution, because these types of queries negatively affect performance.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745537947a4631992.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信