Extend meta query arguments

All we need is an easy explanation of the problem, so here it is.

I have this meta query:

$args = array(
    'post_type' => 'events',
    'meta_query' => array(
        array(
            'key' => 'fl_type',
            'value' => 'Awards'
        )
    ),
    'posts_per_page' => -1,
    'meta_key' => 'fl_order',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'fields' => 'ids'
);

However I need to extend this by another 2 numeric variables fl_expire = 0 and fl_global = 1 and I can’t figure out the correct syntax for this.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Try this

$args = array(
    'post_type' => 'events',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'fl_type',
            'value' => 'Awards'
        ),
        array(
            'key' => 'fl_expire',
            'value' => 0,
            'type' => 'NUMERIC',
        ),
        array(
            'key' => 'fl_global',
            'value' => 1,
            'type' => 'NUMERIC',
        ),
    ),
    'posts_per_page' => -1,
    'meta_key' => 'fl_order',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'fields' => 'ids'
);

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply