1

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`
1
  • 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? Commented Feb 24, 2011 at 21:35

3 Answers 3

7

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

0

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.

0

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 agree to our terms of service and acknowledge you have read our privacy policy.

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