0

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>';
    }
}

1 Answer 1

2

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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