I can't seem to get this to work — I'm trying to show posts that have the meta 'featured_image' value set to anything. From what I can tell, it's set up correctly, but I'm in a little over my head. Here's what I have:

    global $post;
    $myposts = get_posts(array(
      'showposts' => 5,
      'offset' => 7,
      'meta_query' => array(
          'key' => 'featured_image',
          'value' => '',
          'compare' => 'NOT LIKE'
    foreach($myposts as $post) {
      $meta = get_post_meta($post->ID, '');
      <a href=""><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php if (isset($meta['featured_image'][0]) && !empty($meta['featured_image'][0])): echo $meta['featured_image'][0]; else: ?>/wp-content/themes/SSv2011/images/review-default.gif<?php endif; ?>&w=84&h=60" alt="" /></a>
  <?php unset($myposts); ?>
  <?php } ?>

Updated to show the entire query. Any ideas? :\

Updated to WP_Query and I'm getting posts to appear, but they're not ONLY posts that have something inside 'featured_image'. Any ideas with that? The new query:

      $args = array(
        'showposts' => 5,
        'meta_query' => array(
            'key' => 'featured_image',
            'value' => '',
            'compare' => '!='
      $ft_pagination = new WP_Query( $args );
      <?php while ($ft_pagination->have_posts()) : $ft_pagination->the_post(); ?>
        <?php $ftimage = get_post_meta(get_the_id(), 'featured_image', TRUE); ?>
            <a href="">
            <?php if ($ftimage): ?>
              <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $ftimage; ?>&w=84&h=60" alt="" />
            <?php else: ?>
              <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=/wp-content/themes/ssv/images/review-default.gif&w=84&h=60" alt="" />
            <?php endif; ?>

  • Why not use wp_query – Bainternet Mar 1 '11 at 16:15
  • compare is spelt wrong in the above(and your get_post_meta call is missing a key), are either of those the problem? (if you want to look at pages, you'll need to set the post type). – t31os Mar 1 '11 at 16:17
  • Just caught that — Thanks! Unfortunately it didn't solve the problem. The missing get_post_meta key is the $single which doesn't need set, right? It works for every other query I've created. – robalan Mar 1 '11 at 16:21
  • @bainternet I didn't set these up originally, so I'm not sure... Like I said — I'm in a little over my head. ;o) – robalan Mar 1 '11 at 16:23
  • 2
    Well meta_query works fine for me with get_posts, but no i meant the second get_post_meta parameter in your call which is suppose to be the meta_key to get data for. If you're wanting to check pages you'll need to explicitly set that as the post type in the args. – t31os Mar 1 '11 at 16:24

Not possible with an empty value. See: How can I show posts only if meta_value is not empty

  • 1
    This is fixed in WP 3.2 alpha. – scribu Apr 25 '11 at 11:03

Firstly your get_post_meta call doesn't have a key, shouldn't that be get_post_meta( $post->ID, 'featured_image', true ) or get_post_meta( $post->ID, 'featured_image', false ) if you want multiple values.

Additionally you're treating the results of the get_post_meta call like it has an array of values here $meta['featured_image'][0], yet you've not specified a key, meaning it's highly unlikely to contain anything at that point.

I suspect the lack of key in the get_post_meta call is the main issue.

Addtitionally i'd suggest moving back to the != operator, NOT LIKE wouldn't really be appropriate for an empty string comparison, imagine how that SQL will look.

WHERE meta_value NOT LIKE '%%'

Where as using the not equals comparison we get..

WHERE meta_value != ''

..which is(i believe) exactly what we intend to check for, a meta field with a value that is anything but an empty value.

  • Tried it — got nothing. :o\ Is this even possible within get_posts? Someone else mentioned I may need to use query_posts. – robalan Mar 1 '11 at 17:05
  • It works for me, dump some of your vars and check they contain what you're expecting them to, eg. $meta and $myposts – t31os Mar 1 '11 at 17:15
  • $meta contains [featured_image] => Array ( [0] => /wp-content/uploads/2011/02/ThisImage.jpg - Seems to be there... – robalan Mar 1 '11 at 18:10
  • Ok @t31os - I've updated my query to WP_Query and am now setting the key... I'm now getting posts returned, but it's grabbing any post - not just the ones with a value in featured_image. Edited the post to show the new query above. – robalan Mar 2 '11 at 3:19
  • Still works for me just fine, try passing the surpress filters arg, eg. 'surpress filters' => true, to bypass any filters that might be hooked on by a plugin or function, and again remember to explicitly set the post type if you're fetching anything not posts, eg. 'post_type' => 'your-type'. – t31os Mar 2 '11 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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