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.


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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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