'Wordpress Featured Images not appearing using SQL
I am trying to make a query to get the post title, link and images of the post. I have what I need with the following query:
SELECT DISTINCT Users.ID, Users.display_name, PostObject.post_title, PostObject.post_type, Term3.name, Term2.taxonomy FROM `wp_users` AS `Users`
INNER JOIN `wp_3_learndash_user_activity` AS `Learndash`
ON Users.ID = Learndash.user_id
INNER JOIN `wp_3_posts` AS `PostObject`
ON PostObject.ID = Learndash.post_id
INNER JOIN `wp_3_postmeta` AS `Postmeta`
ON (PostObject.ID = Postmeta.post_id
AND Postmeta.meta_value IS NOT NULL
AND Postmeta.meta_key = '_thumbnail_id'
)
INNER JOIN `wp_3_postmeta` AS `Postmeta2`
ON (Postmeta.meta_value = Postmeta2.post_id
AND Postmeta2.meta_key = '_wp_attached_file'
AND Postmeta2.meta_value IS NOT NULL
)
INNER JOIN `wp_3_term_relationships` AS `Term1`
ON PostObject.ID = Term1.object_id
INNER JOIN `wp_3_term_taxonomy` AS `Term2`
On Term2.term_taxonomy_id = Term1.term_taxonomy_id
INNER JOIN `wp_3_terms` AS `Term3`
ON Term3.term_id = Term2.term_id
WHERE Users.ID = '1' AND PostObject.post_status='publish' GROUP BY Term3.name
ORDER BY `Term3`.`name` ASC
I am using get_the_post_thumbnail() method to display featured images:
function display_courses(){
try{
global $wpdb, $post;
$current_user = wp_get_current_user();
$enroll_query = $wpdb->get_results( $wpdb->prepare("SELECT DISTINCT Users.ID, Users.display_name, PostObject.post_title, PostObject.post_type, Term3.name, Term2.taxonomy FROM `wp_users` AS `Users`
INNER JOIN `wp_3_learndash_user_activity` AS `Learndash`
ON Users.ID = Learndash.user_id
INNER JOIN `wp_3_posts` AS `PostObject`
ON PostObject.ID = Learndash.post_id
INNER JOIN `wp_3_postmeta` AS `Postmeta`
ON (PostObject.ID = Postmeta.post_id
AND Postmeta.meta_value IS NOT NULL
AND Postmeta.meta_key = '_thumbnail_id'
)
INNER JOIN `wp_3_postmeta` AS `Postmeta2`
ON (Postmeta.meta_value = Postmeta2.post_id
AND Postmeta2.meta_key = '_wp_attached_file'
AND Postmeta2.meta_value IS NOT NULL
)
INNER JOIN `wp_3_term_relationships` AS `Term1`
ON PostObject.ID = Term1.object_id
INNER JOIN `wp_3_term_taxonomy` AS `Term2`
On Term2.term_taxonomy_id = Term1.term_taxonomy_id
INNER JOIN `wp_3_terms` AS `Term3`
ON Term3.term_id = Term2.term_id
WHERE Users.ID = '".$current_user->ID."' AND PostObject.post_status='publish' GROUP BY Term3.name
ORDER BY `Term3`.`name` ASC"));
//Check if values is duplicate from any tables
if ($enroll_query) {
foreach ($enroll_query as $enrollresults) {
setup_postdata($enrollresults);
echo $enrollresults->post_title;
echo get_the_post_thumbnail($enrollresults->ID, 'thumbnail');
}
wp_reset_postdata();
}
}
catch(Exception $e)
{
echo 'Error writing to database: ', $e->getMessage(), "\n";
}
}
However when I run my SQL query, no thumbnail images are appearing on the front page even I call the table column meta_key field _thumbnail_id. Is there a way to show the thumbnail on the site?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
