Does meta_query work within get_posts array?

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:

 <ul>
  <?php
    global $post;
    $myposts = get_posts(array(
      'showposts' => 5,
      'offset' => 7,
      'meta_query' => array(
        array(
          'key' => 'featured_image',
          'value' => '',
          'compare' => 'NOT LIKE'
          )
        )
    ));
    foreach($myposts as $post) {
      setup_postdata($post);
      $meta = get_post_meta($post->ID, '');
  ?>
    <li>
      <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>
    </li>
  <?php unset($myposts); ?>
  <?php } ?>
  </ul>

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:

      <?php
      $args = array(
        'showposts' => 5,
        'meta_query' => array(
          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); ?>
        <li>
          <article>
            <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; ?>
            </a>
          </article>
        </li>
      <?php
      endwhile;

      wp_reset_query();
      ?>

2 Answers
2

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

Leave a Comment