loop query exclude meta_key with meta_value

Do NOT use query_posts() – it is bad, very bad to be exact.

For more information read:

Use WP_Query instead.

Example with meta_key, meta_value and meta_compare parameters in use:

$args = array(
    'meta_key'     => 'clpr_excoupon',
    'meta_value'   => '0',
    'meta_compare' => '!='
);
$query = new WP_Query( $args );

Note: those parameters work for query_posts too, but as said, you shouldn’t use it.


Update:

With NOT EXISTS compare.

$args = array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'clpr_excoupon',
            'compare' => 'NOT EXISTS',
            'value'   => 'prior to WP 3.9 a value was needed due to bug #23268'
        ),
        array(
            'key'     => 'clpr_excoupon',
            'compare' => '!=',
            'value'   => '0'
        ),
    ),
);
$query = new WP_Query( $args );

Leave a Comment