2

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

3

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')
0

From the reference of your first $args

$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' )
    )
);

You have added 'relation' => 'OR', which is not needed. The relation needs to be added if you have multiple meta-value to be queried. Also compare is not needed if you want a specific value, as by default is uses =.

The updated $args will be

$args = array(
    'post_type' => 'cqpp_interventions',
    'posts_per_page' => '-1',
    'meta_query' => array(
            array(
                'meta_key' => 'priority',
                'meta_value' => '80', // since 80 is string
             )
    )
);

// try either of below.
$myPost = new WP_Query( $args ); // fetch post and managed in objects
$myPost = get_posts( $args ); // fetch post and store in array

Hope this will help you out.

Thanks

1
  • In meta_query you use 'key' => and 'value' => not use 'meta_key' => and 'meta_value' => like Chin Leung answer Dec 4, 2017 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.