+91-40 – 420 001 24 info@dhrusya.com

WordPress meta queries

If you want to get a post with meta key show_on_homepage and meta value on, you can do it in the following way:

1
2
3
4
5
6
$rd_args = array(
    'meta_key' => 'show_on_homepage',
    'meta_value' => 'on'
);
 
$rd_query = new WP_Query($rd_args);

If you need to query all posts except the ones with this pair of meta key and value, you can use the following parameters:

1
2
3
4
5
6
7
$rd_args = array(
    'meta_key' => 'show_on_homepage',
    'meta_value' => 'on',
    'meta_compare' => '!='
);
 
$rd_query = new WP_Query( $rd_args );

Get Posts with a Specific Custom Field Value

1
2
3
4
5
6
7
8
9
10
// the meta_key 'color' with the meta_value 'white'
$rd_args = array(
    'meta_query' => array(
        array(
            'key' => 'color',
            'value' => 'white'
        )
    )
);
$rd_query = new WP_Query( $rd_args );

Get all the posts except the ones with meta key «color» and meta value «white»:

1
2
3
4
5
6
7
8
9
10
11
$rd_args = array(
    'meta_query' => array(
        array(
            'key' => 'color',
            'value' => 'white',
            'compare' => '!='
        )
    )
);
 
$rd_query = new WP_Query( $rd_args );

Get all the posts with white OR green color custom field value:

1
2
3
4
5
6
7
8
9
10
11
// custom field name is color and custom field value is 'white' OR 'green'
$rd_args = array(
    'meta_query' => array(
        array(
            'key' => 'color',
            'value' => array('white','green'),
            'compare' => 'IN'
        )
    )
);
$rd_query = new WP_Query( $rd_args );

Get all the posts (products in online shop for example) except white products and green products:

1
2
3
4
5
6
7
8
9
10
$rd_args = array(
    'meta_query' => array(
        array(
            'key' => 'color',
            'value' => array('white','green'),
            'compare' => 'NOT IN'
        )
    )
);
$rd_query = new WP_Query( $rd_args );

Get Posts Within a Given Range of Numeric Meta Values

1
2
3
4
5
6
7
8
9
10
11
12
// the product price is more than 2000 and less than 4000
$rd_args = array(
    'meta_query' => array(
        array(
            'key' => 'price',
            'value' => array( 2000, 4000 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
);
$rd_query = new WP_Query( $rd_args );

Numeric Comparison

1
2
3
4
5
6
7
8
9
10
$rd_args = array(
    'meta_query' => array(
        array(
            'key' => 'price',
            'value' => 2000,
            'type' => 'numeric',
            'compare' => '>='
        )
    )
);
The product price is less than 4000:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$rd_args = array(
    'meta_query' => array(
        array(
            'key' => 'price',
            'value' => 4000,
            'type' => 'numeric',
            'compare' => '<'
        )
    )
);
$rd_query = new WP_Query( $rd_args );
&#91;/php&#93;
 
<h4>Query Posts by Several (two or more) Custom Field Values</h4>
 
[php]
$rd_args = array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'color',
            'value' => 'white'
        ),
        array(
            'key' => 'price',
            'value' => array( 2000, 4000 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
);
$rd_query = new WP_Query( $rd_args );