WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using wordpress MU 3.3.1. I want to add a function within a loop that displays only posts with image, or featured image.

please help me on how to filter posts.

<?php get_header(); ?>
<div id="content">
    global $post;
    $categories = get_categories('child_of=7694');  
    foreach ($categories as $category) {

        $cat_id = $category->cat_ID;

        $games = get_posts('numberposts=6&orderby=rand&category='.$cat_id);

        if ($games) : ?>

          <div class="single">

            <h1><a href="<?php echo get_category_link($cat_id); ?>"><?php echo $category->name; ?></a></h1>

           <?php foreach ($games as $post) : $do_not_duplicate = $post->ID; ?>

              <div class="game_title">

                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo get_post_meta($post->ID, 'mabp_description', $single = true); ?>" >


                if ( (strlen($post->post_title) > 10) ) { 

                  echo substr($post->post_title, 0, 10).".."; 


                else {





                <br />

                <a href="<?php the_permalink() ?>" class="thumb_link" rel="bookmark" title="<?php echo get_post_meta($post->ID, 'mabp_description', $single = true); ?>"><?php 
        'name' => 'gab_featured',
        'enable_video' => 'true',
        'catch_image' => 'true',
        'enable_thumb' => 'true',
        'resize_type' => 'c', 
        'media_width' => '60', 
        'media_height' => '34', 
        'thumb_align' => 'featured_thumb', 
        'enable_default' => get_option('of_nw_enfea2'),
        'default_name' => 'featured_thumb.jpg'                                  

            <?php endforeach; ?>

            <div class="cat_link"><a href="<?php echo get_category_link($cat_id); ?>">More Games</a></div>


        <?php endif; }?>

    </div> <?php // End content div ?>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>
share|improve this question
You will want to look into has_post_thumbnail, WP_Query. I've written answers to do this sort of thing a dozen times before. Here's another more complex approach Cheers! – David Apr 7 '12 at 6:04
global $wpdb;

$posts = $wpdb->get_results(" SELECT * FROM `wp_posts` p inner join `wp_posts` i on i.id=p.id and p.post_type='post' and i.post_type='attachment' where i.post_parent=p.id ");
share|improve this answer
i have put my codes in my question. please can you elaborate your answer using my codes? @Rajeev Vyas – heradius Apr 7 '12 at 7:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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