• Hi there,

    I am trying to select all pages (or posts) from the database where a featured image (previously called Post Thumbnails) has been added.

    However I cannot seem to find the right parameter for this, is it even possible like this? If not, how can I do this differently?

    Thanks for the help!

    <?php
    $featured = new WP_Query('post_type=page&orderby=title&order=ASC&post_status=publish');
    
    while ($featured->have_posts()) : $featured->the_post();$do_not_duplicate = $post->ID;
    
    echo $post->post_title." - ".wp_get_attachment_url( get_post_thumbnail_id($post->ID) )."<br />";
    
    endwhile;
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter xanuex

    (@xanuex)

    Is there anyone who can help me with this mind breaker 🙂

    Thread Starter xanuex

    (@xanuex)

    My solution, if someone has a better suggestion, please let me know!

    <?php
    //The Query
    $my_query = new WP_Query('post_type=page&orderby=title&order=ASC&post_status=publish&showposts=-1');
    
    //The Loop
    while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;
    
    	//Add pages with thumbnail to an array
    	if (has_post_thumbnail()) {
    		$image[$count++] = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    	}
    
    endwhile;
    
    //Reset Query
    wp_reset_query();
    ?>
    
    <?php foreach ($image as $i => $value) : ?>
    ...
    
    <?php endforeach; ?>

    Ok, I think I got it figured out. It took a little digging through the database to find where the meta information was stored. It’s in the wp_postmeta table. I did a full dump of that table and looked for the file name of my featured image. I found it with meta_key _wp_attached_file. A couple rows below it there was the a meta_key called _thumbnail_id with a value of the post id in question. So here is my query:

    <?php query_posts('posts_per_page=3&meta_key=_thumbnail_id'); ?>

    xanuex I need to cantact you, please send mi an email to consmint ad yahoo dot es

    Thanks and excuse me to use this channel

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Select pages with featured image with query’ is closed to new replies.