Search code examples
wordpresscustom-post-typeadvanced-custom-fieldsmeta-query

ACF checkbox meta query


The below isn't working, can't see why. is_focus_product is a True/False ACF field

$ls = get_posts([
    'meta_query' => [
        [
            'key' => 'is_focus_product', 'value' => 1, 'compare' => '='
        ]
    ]
]);
// array(0){}

However this returns as expected...

var_dump(get_field('is_focus_product', 36));
// bool(true)

Solution

  • I found a solution but IMO it's a bullshit WordPress flaw, it shouldn't be restricted to a certain post type because you should have the freedom (in this case anyway, I reckon there are billions of similar use cases) to filter as necessary (they are ALL posts after all...).

    You need the post type, so...

    $ls = get_posts([
        'post_type' => 'products',
        'meta_query' => [
            [
                'key' => 'focus_product', 'value' => '1', 'compare' => '='
            ]
        ]
    ]);