• I’m having a very hard time getting a custom loop to display posts if they have a featured image associated with it. After fifty different variations, I’m lost, and would really appreciate it if anyone can guide me about what it is that I’m doing wrong.

    Here is my code:

    <?php $query = new WP_Query( array( 'post_type' => array( 'post', 'page' ) ) ); ?>
     <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    	<?php if ( has_post_thumbnail()) : ?>
       	<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
       	<?php the_post_thumbnail( array( 1000, 315), array( 'class' => 'slideshow-img' ) ) ?></a>
    	<?php endif; ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

    I’m querying to see if a certain post/page has a featured image with this code.

    I used the exact same variation but with the get_posts() function and that worked perfectly, however, it doesn’t show any pages with featured images, hence a dead end again.

    Is there anything I’m doing wrong? Without the post thumbnail condition, the query works. Is there anything I’m missing?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter kamrankhan

    (@kamrankhan)

    correction

    Sorry about this but I just realsied that the code works, but it only shows the first featured post. It doesn’t move forward with other posts that have featured images associated with it.

    Thread Starter kamrankhan

    (@kamrankhan)

    So this works:

    <?php $my_query = new WP_Query( array ( 'order' => 'DESC' ) ); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	<?php setup_postdata($post); ?>
    	<?php if ( has_post_thumbnail()) : ?>
       		<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
       		<?php the_post_thumbnail( array( 1000, 315), array( 'class' => 'slideshow-img' ) ) ?></a></li>
    		<?php endif; ?>
    <?php endwhile; ?>

    Except, it won’t show any pages with featured images. UGGHHH.. This should be dementedly easy; what am I doing wrong?

    Thread Starter kamrankhan

    (@kamrankhan)

    this worked:

    $postslist = get_posts( array( 'numberposts' => 10, 'order'=> 'DESC', 'orderby' => 'title', 'post_type' => 'any' ) );
    foreach ($postslist as $post) :  setup_postdata($post); ?>
    						<?php if ( has_post_thumbnail()) : ?>
       							<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
       							<?php the_post_thumbnail( array( 1000, 315), array( 'class' => 'slideshow-img' ) ) ?>
       							</a></li>
    						<?php endif; ?>
    					<?php endforeach; ?>

    but it only displayed the first three posts. Changing numberposts to 1000 displays all of them (there are only 10 posts with featured images assigned’. What the fudge is wrong with my code?

    There’s nothing wrong with your code. It’s your interpretation/logic that is flawed. Right now, you can’t build a custom query that will only grab posts with a featured image. So you need to grab all posts and then loop through them – checking for featured images.

    Thread Starter kamrankhan

    (@kamrankhan)

    Damn, that makes sense.

    However, why does the previous code not work? Shouldn’t the query go through all the posts and organize and then display only the ones with featured images?

    You’re only pulling 10 posts.

    On the off chance that this post is found by someone looking for the same info – the Thumbnail data is held in a custom field (postmeta) so can be found by testing if the post has meta with a key of _thumbnail_id using a meta_query

    Above query would be:

    <?php $my_query = new WP_Query( array (
        'order' => 'DESC',
        'meta_query' => array(
            array( 'key' = > '_thumbnail_id')
        )
    )); ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘wp_query and featured image problem’ is closed to new replies.