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? :

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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.