Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I cannot get this simple meta query to work and I have no idea why.

I have a custom post type registered, called 'ef_project'. I have a few test posts created using post type, each with a postmeta field called 'project_status' - the values can be 'draft', 'inactive', 'expired' and I have created a few posts with each value.

The query DOES return the 'ef_project' posts I created, but does not filter the list down to posts with a project_status meta field of 'draft' - instead it displays any ef_project post regardless of its 'project_status' meta field value. What am I doing wrong here?

// The Query
$the_query = new WP_Query(
    array(
    'post_type' => 'ef_project', 
    'meta_query' =>
        array(
            array(
                'key' => 'project_status',
                'value' => 'draft',
                'type' => 'string',
                'compare' => '='
            )
        ),
    )
);

// Test Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . ' -  Status: ' . get_post_meta(get_the_ID(), 'project_status', true) .  '</li>';
    }
}
share|improve this question
add comment

1 Answer

It seems the 'type' => 'string' is not right

From the codex

type (string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.

try and remove the above line from the code and check or try using the below instead.

'type' => 'CHAR'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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