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'm trying to show result for a custom field that is not empty on a custom post type but getting no results?

                    <?php

                        if (have_posts()) :
                            $args = array(
                                'post_type' => 'programmes',
                                'meta_query' => array(
                                        'key' => 'linktovideocatchup',
                                        'value' => '',
                                        'compare' => 'NOT LIKE'),
                                //'caller_get_posts' => 1,                              
                                );
                        ?>

                        <?php query_posts( $args ); ?>


                        <?php while (have_posts()) : the_post(); ?> `enter code here`
share|improve this question
    
I'll ask the obvious question: have you looked at the wp_post_meta table in the DB (using phpMyAdmin or whatever) and verified that you have one (or more) such items? If so, have you then checked the corresponding id(s) in wp_posts and verified that at least one of them has a post_type of programmes? –  Peter Rowell Feb 24 '11 at 21:35
add comment

3 Answers

up vote 7 down vote accepted

you're missing an array within the meta_query element:

$args = array(
           'post_type' => 'programmes',
           'meta_query' => array(
                              array(
                                   'key'     => 'linktovideocatchup',
                                   'value'   => '',
                                   'compare' => 'NOT LIKE'
                                   )
                              )
            );

(this is required to allow for querying of multiple meta fields.)

you also had an extraneous comma after the meta_query array element which can cause problems.

i think you should also be able to use the operator '<>' rather than 'NOT LIKE', i believe it's more efficient.

there's a good write-up on the meta_query functionality here: http://scribu.net/wordpress/advanced-metadata-queries.html

share|improve this answer
add comment

Try this code:

$args = array(
       'post_type' => 'programmes',
       'meta_query' => array(
                          array(
                               'key'     => 'linktovideocatchup',
                               'value'   => '',
                               'compare' => '!='
                               )
                          )
        );

Just change 'NOT LIKE' by '!='. It worked for me ;)

share|improve this answer
add comment

There is a better answer at How can I show posts only if meta_value is not empty - Can't query a null value like this.

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.