I use a standard loop in my index.php and that shows all the posts beeing posted. What i'm looking for is a code that only shows the posts that have image(s) in it.

With other words: i only want to show posts that have images in content in my loop.

2 Answers 2


I found and modified a chunk of code found here: http://www.wprecipes.com/wordpress-tip-detect-if-a-post-has-at-least-one-image.

while ( have_posts() ) : the_post();
    $content = $post->post_content;
    $searchimages = '~<img [^>]* />~';
    preg_match_all( $searchimages, $content, $pics );
    $iNumberOfPics = count($pics[0]);
    if ( $iNumberOfPics > 0 ) {
        //your loop content goes here

I think this just looks for images entered in the rich text editor - if you want to check for featured images as well you'll likely have to add something. Check out has_post_thumbnail.

  • This does not check whether the post has any text based content. It only checks for the existence of an image. You would need to extend the regular expression within $searchimages variable to allow for this.
    – Adam
    Commented May 20, 2012 at 15:08
  • Perhaps you should change the wording of your question to indicate this requirement? I'll give it some thought Commented May 20, 2012 at 15:21
  • 1
    Its not my question and it could very well be that I've misinterpreted his question as he does not indicate whether this applies to text, text and image or image only. If its the former, then your answer would be a plausible solution.
    – Adam
    Commented May 20, 2012 at 15:28
  • Appologies, thought you were OP. From his question, it sounds like he only wants to check if theres an image in the post, not that the post only contains images. Commented May 20, 2012 at 15:28
  • Yup, I interpret his question differently, but in retrospect you are probably right. Maybe, if you'd like, extend your answer to show him how to use the above conditional check with his query. Thus other people who may stumble upon this in the future will gain some additional benefit too.
    – Adam
    Commented May 20, 2012 at 15:30

I would suggest, that the easiest way to go about this is to create a special category called for example, image-post or something to that effect and assign your post to that category and of course any other applicable categories in which it also may apply to.

Then you can create a loop which queries your image-post category, thus it will show only those posts in which have been assigned to this special category. In effect producing the result you require.

Depending on your needs you can do this other ways, for example by using either..

Custom fields

This allows you to create a custom field meta_key/meta_value which you can use to signify the posts special condition, then you can query posts via meta_key/meta_value.

Custom Meta-Boxes (for advanced customization of your custom fields)

This allows you to give some give some better control over the display and use of custom_fields, essentially opening up access to a variety of UI controls (buttons, fields, text areas, select boxes, drop downs, radio buttons, checkboxes and more) for which you can control certain meta_values to your desire. Think of this as something that sits atop your custom_fields.

Use a custom post type especially for these types of posts

With custom post types, you can create a post type just for your image posts, you might call it something like Visuals or whatever you like. They operate just like Posts and Pages do but are custom to your needs.

There might be a plugin out there that helps you achieve similar results too but since I rarely if ever use plugins (unless forced to) I wouldn't know. In any case I think a plugin would be overkill too in this situation.

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.