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

This script makes a loop showing only posts with images, but I have a problem. It doesnt show the right ammount of posts pr page. Probably because it counts the total nr of posts, including the ones without pictures. Does anyone know how to fix this?

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

Second problem: It also shows broken-image if the image in the post is external.

share|improve this question

I think that the only way is to query the database with LIKE statement and get all IDs and then create a separate WP_Query object for the list of post ID's. Check this guide for example.

share|improve this answer
I'm sorry Mario but my knowlage is not there yet :( can you help? – Demilio May 29 '12 at 7:28
it's a bit of manual work, I'm personally using the Views WP plugin that does a lot of querying and has pagination parameters – Mario Peshev May 30 '12 at 11:27
ok, thanks for the tip :) – Demilio May 31 '12 at 8:50

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.