wordpressadvanced-custom-fields

WP_Query based on value in array in meta field


I am trying to perform a WP Query using the value within an array to exclude certain rows.

I have an event table with a event_status field that is type 'Radio Button' setup as follows.

0 : Provisional
1 : Active
2 : Postponed
3 : Cancelled

I have the return value set to Both.

Therefore when I use get_field('event_status') I get returned the following.

[24-Nov-2024 14:35:27 UTC] {"value":"0","label":"Provisional"}

What I'm looking to do is query only rows where the label is something other than Provisional.

Currently the query is

$events = new WP_Query(Array(
    'posts_per_page' => 3,
    'post_type' => "event",
    'meta_key' => 'event_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        'relation' => 'AND',
        'date_clause' => array(
            'key' => 'event_date',
            'compare' => '>=',
            'value' => $today->format('Ymd'),
            'type' => 'DATE',
        ),
        'status_clause' => array(
            'key' => 'event_status',
            'compare' => '!=',
            'value' => 'Provisional'
        )
    )
));

which I know isn't correct. I have trawled the web but can't find anything specific to allow searching based on a value inside a returned array using the array element (ie 'label' == 'Provisional')

Anyone able to help please?


Solution

  • You can just query for value not equal to 0(value) instead of the label(Provisional)

    $events = new WP_Query(array(
    'posts_per_page' => 3,
    'post_type' => "event",
    'meta_key' => 'event_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        'relation' => 'AND',
        'date_clause' => array(
            'key' => 'event_date',
            'compare' => '>=',
            'value' => $today->format('Ymd'),
            'type' => 'DATE',
        ),
        'status_clause' => array(
            'key' => 'event_status',
            'compare' => '!=',
            'value' => '0'  // Looking for the value, not the label
        )
    )));
    

    I tested locally without the date_clause as I didn't add an event_date field and it works fine.