I found this code online. Its a code that only shows posts with images inside. Now, the problem is that if you set Wordpress to show 50 posts, this code shows the posts with the images, but also counts the ones without.

With other words.. if you got 50 posts on your frontpage where 6 are with images this code shows only 6 posts.

Does anyone know how I can make it show 50 posts (the same as set in WordPress) with images?

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 haven't tried it, but I think that one proper solution could be to write a simple function that runs after the post is published (on save) and checks wether this post has images inside or not.If it has maybe is good to update a custom field in the post that will be a boolean and will check if it this post has images or not.This way you can do a WP Query afterwards and get the posts with images using a meta_key query.

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.