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`
  • 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
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

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.

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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