Title: Problems with has_post_thumbnail
Last modified: August 19, 2016

---

# Problems with has_post_thumbnail

 *  [grumo64](https://wordpress.org/support/users/grumo64/)
 * (@grumo64)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/problems-with-has_post_thumbnail/)
 * I want to display my last post by category **but only** if the post has thumbnail.
 * I need something like this :
 *     ```
       $categories = get_categories('orderby=id&child_of=10');
       foreach($categories as $cat) {
         $postslist = get_posts('category=10&numberposts=1&order=date');
         foreach ($postslist as $post) {
           if (has_post_thumbnail($post->ID)){
             ... the last post thumbnail
           }else{
             ... find the next post id with thumbnail
           }
         }
       }
       ```
   
 * Maybe the “get_adjacent_post” could help ?
    Thanks for your help

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [grumo64](https://wordpress.org/support/users/grumo64/)
 * (@grumo64)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/problems-with-has_post_thumbnail/#post-1739863)
 * Please any help could help me …
 *  [ascottmccauley](https://wordpress.org/support/users/ascottmccauley/)
 * (@ascottmccauley)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/problems-with-has_post_thumbnail/#post-1740045)
 * Probably a little too drunk to be posting code right now, but maybe this will
   hep you out:
 *     ```
       //Previous Post in Category
       function get_prev_cat_post($categoryID) {
           global $wpdb, $post;
   
           // get post date of current post
           $currentPostDate = $post->post_date;
       	$newPostInfo = $wpdb->get_row(
       		"SELECT * FROM $wpdb->posts
       		LEFT JOIN $wpdb->term_relationships ON
       		($wpdb->posts.ID = $wpdb->term_relationships.object_id)
       		LEFT JOIN $wpdb->term_taxonomy ON
       		($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
       		WHERE $wpdb->posts.post_status = 'publish'
       		AND $wpdb->posts.post_type = 'post'
       		AND $wpdb->term_taxonomy.taxonomy = 'category'
       		AND $wpdb->term_taxonomy.term_id = $categoryID
       		AND $wpdb->posts.post_date < '$currentPostDate'
       		ORDER BY post_date DESC
       		LIMIT 1");     
   
       	if ($newPostInfo) {
       		return $newPostInfo;
       	}
       }
   
       //Next Post in Category
       function get_next_cat_post($categoryID) {
           global $wpdb, $post;
   
           // get post date of current post
           $currentPostDate = $post->post_date;
       	$newPostInfo = $wpdb->get_row(
       		"SELECT * FROM $wpdb->posts
       		LEFT JOIN $wpdb->term_relationships ON
       		($wpdb->posts.ID = $wpdb->term_relationships.object_id)
       		LEFT JOIN $wpdb->term_taxonomy ON
       		($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
       		WHERE $wpdb->posts.post_status = 'publish'
       		AND $wpdb->posts.post_type = 'post'
       		AND $wpdb->term_taxonomy.taxonomy = 'category'
       		AND $wpdb->term_taxonomy.term_id = $categoryID
       		AND $wpdb->posts.post_date > '$currentPostDate'
       		ORDER BY post_date ASC
       		LIMIT 1");
   
       	if ($newPostInfo) {
       		return $newPostInfo;
       	}
       }
   
       //Previous & Next Post Navigation with images.
       function get_navHTML() {
       	$navHTML = '';
       	if(function_exists('get_prev_cat_post') && function_exists('get_next_cat_post')) {
       		$featuredID = get_category_by_slug('featured')->term_id;
       		$slideshowID = get_category_by_slug('slideshow')->term_id;
       		$portfolioID = get_category_by_slug('portfolio')->term_id;
       		$excludedCats = array($featuredID, $slideshowID, $portfolioID);
       		foreach (get_the_category() as $category) {
       			if (!in_array($category->cat_ID,$excludedCats)) {
       				$mainCatID = $category->cat_ID;
       			}
       		}
       		$mainCat = get_cat_name($mainCatID);
   
       		$prev = get_prev_cat_post($mainCatID);
       		$prevID = $prev->ID;
       		$prevTitle = $prev->post_title;
       		$prevURL = get_permalink($prevID);
       		$image = get_post_meta($prevID, 'photoQImageSizes', true);
       		if($image){
       			$prevThumb = $image['tiny']['imgTag'];
       		}elseif(has_post_thumbnail($prevID)) {
       			$prevThumb = get_the_post_thumbnail($prevID,'thumbnail');
       		}
       		$next = get_next_cat_post($mainCatID);
       		$nextID = $next->ID;
       		$nextTitle = $next->post_title;
       		$nextURL = get_permalink($nextID);
       		$image = get_post_meta($nextID, 'photoQImageSizes', true);
       		if($image){
       			$nextThumb = $image['tiny']['imgTag'];
       		}elseif(has_post_thumbnail($nextID)) {
       			$nextThumb = get_the_post_thumbnail($nextID,'thumbnail');
       		}
       		if($prev || $next){
       			$navHTML .= '
       			<nav class="nav-single">
       				<h6><a href="'. get_category_link($mainCatID) .'" title="'. $mainCat .'">More from <em>'. $mainCat . '</em></a></h6>
       				<ul>';
       					if($prev){
       						$navHTML .= '
       						<li class="older">
       							<a href="' . $prevURL . '" title="' . $prevTitle . '">
       								<span class="nav-image">' . $prevThumb . '</span>
       							</a>
       						</li>';
       					}
       					if($next){
       						$navHTML .= '
       						<li class="newer">
       							<a href="' . $nextURL . '" title="' . $nextTitle . '">
       								<span class="nav-image">' . $nextThumb . '</span>
       							</a>
       						</li>';
       					}
       				$navHTML .= '</ul>
       			</nav>';
       		}
       	}
       	return $navHTML;
       }
       ```
   
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/problems-with-has_post_thumbnail/#post-1740046)
 * [http://wordpress.org/support/topic/display-the-post-only-if-thumbnail-is-set-up?replies=17](http://wordpress.org/support/topic/display-the-post-only-if-thumbnail-is-set-up?replies=17)

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Problems with has_post_thumbnail’ is closed to new replies.

## Tags

 * [get_adjacent_post](https://wordpress.org/support/topic-tag/get_adjacent_post/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [Michael](https://wordpress.org/support/users/alchymyth/)
 * Last activity: [15 years, 7 months ago](https://wordpress.org/support/topic/problems-with-has_post_thumbnail/#post-1740046)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
