-
-
Save ssatz/c7000c81b8c60697f7dd3af0f5d47423 to your computer and use it in GitHub Desktop.
SQL Query to get Latest Wordpress Post with Featured Image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select DISTINCT wp_posts.id, wp_posts.post_title, wp_posts.post_content, wp_posts.post_type, featured_image.guid as post_image, wp_posts.post_modified, wp_users.display_name | |
from wp_posts | |
inner join wp_postmeta on wp_posts.id = wp_postmeta.post_id and wp_postmeta.meta_key = '_thumbnail_id' | |
inner join wp_posts as featured_image on featured_image.id = wp_postmeta.meta_value | |
inner join wp_users on wp_users.id = wp_posts.post_author | |
where wp_posts.post_status = 'publish' | |
order by wp_posts.ID desc | |
limit 10 |
how can i get posts from specific category with this query?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
perfect, also if you need post category
$sql = "SELECT DISTINCT
wp_posts.id, wp_posts.post_title, wp_posts.post_name, wp_posts.post_content, wp_posts.post_type, wp_terms.slug, featured_image.guid
AS post_image,
wp_posts.post_modified,
wp_users.display_name
from wp_posts
inner join wp_postmeta on wp_posts.id = wp_postmeta.post_id and wp_postmeta.meta_key = '_thumbnail_id'
inner join wp_posts as featured_image on featured_image.id = wp_postmeta.meta_value
inner join wp_users on wp_users.id = wp_posts.post_author
inner join wp_term_relationships on wp_term_relationships.object_id = wp_posts.id
inner join wp_terms on wp_terms.term_id = wp_term_relationships.term_taxonomy_id
where wp_posts.post_status = 'publish' LIMIT 10";