I am trying to filter the loop to find posts that have a meta_key with a specific meta_value. I’ve looked at the Codex, and I’ve tried the following with no luck:

// No results
$args = array(
    'post_type' => 'cqpp_interventions',
    'posts_per_page' => '-1',
    'meta_query' => array(
            'relation' => 'OR',
            array(
                'meta_key' => 'priority',
                /* Tried this too
                'meta_value' => '80',
                'compare' => '='
                */
                'meta_value' => array('80'),
                'compare' => 'IN' )
    )
);

// No results
$args = array(
    'post_type' => 'cqpp_interventions',
    'posts_per_page' => '-1',
    'meta_key' => 'priority',
    'meta_value' => 80
);

// This list me all cqpp_interventions and I can confirm that I have some with meta_value set to 80
$args = array(
    'post_type' => 'cqpp_interventions',
    'posts_per_page' => '-1',
    'meta_key' => 'priority'
);


$cqpp_posts = get_posts( $args );

Here is how I verify inside the loop:

$priority = get_post_meta( get_the_ID(), 'priority');
echo '<pre>';
var_dump($priority);
echo '</pre>';

which results in:

search.php:16:
array (size=1)
    0 => 
        array (size=1)
            0 => string '80' (length=2)

search.php:16:
array (size=1)
    0 => 
        array (size=2)
        0 => string '80' (length=2)
        1 => string '91' (length=2)

What can I do to fix this?

2 Answers
2

You could try this:

$args = array(
    'post_type' => 'cqpp_interventions',
    'posts_per_page' => '-1',
    'meta_query' => array(
        array(
            'key' => 'priority',
            'value' => '80'
        )
    )
);

The real issue with your query is because you are passing meta_key and meta_value. However, the arrays in your meta_query argument should have the keys key and value instead.

This would also work:

'key' => 'priority',
'value' => array('80')

Leave a Reply

Your email address will not be published. Required fields are marked *