WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get random post which has featured image (3 posts)

  1. holden321
    Member
    Posted 9 months ago #

    How can I get random post which has featured image?

  2. graphicgeek
    Member
    Posted 9 months ago #

    This should do it:

    <?php 
    
    	$args = array(
    		'post_type' => 'post',
    		'orderby' => 'rand',
    		'posts_per_page' => 1,
    		'meta_key' => '_thumbnail_id'
    	);
    	$query = new WP_Query($args);
    
    	while ( $query->have_posts() ) { $query->the_post();
    		//do stuff here (the_title() or the_content(), etc)
    	} wp_reset_postdata(); 
    
    ?>
  3. holden321
    Member
    Posted 9 months ago #

    Thank you!

Reply

You must log in to post.

About this Topic

Tags

No tags yet.