> For the complete documentation index, see [llms.txt](https://jks.gitbook.io/khoa-hoc-database-trong-wordpress/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jks.gitbook.io/khoa-hoc-database-trong-wordpress/bai-5-thuc-hanh-query-du-lieu-database-wordpress/truy-van-sql-database-wordpress-get-10-bai-viet-moi-nhat-co-hinh-dai-dien.md).

# Truy vấn sql database wordpress get 10 bài viết mới nhất có hình đại diện

<pre class="language-php"><code class="lang-php">&#x3C;?php 
<strong>        global $wpdb;
</strong>	$table 	= $wpdb->prefix . 'posts';
	$tablemeta 	= $wpdb->prefix . 'postmeta';
	$offset 	= 0;
	$limit 	        = 10;
	$sql = "SELECT  `post`.`ID`,
					`post`.`post_author` as author,
					`post`.`post_date` as date,
					`post`.`post_title` as title,
					`post`.`post_name` as slug,
					`thumb_link`.`meta_value` as image
	FROM $table as post
	LEFT JOIN $tablemeta as thumb_id ON `thumb_id`.`post_id` = `post`.`ID` AND `thumb_id`.`meta_key` = '_thumbnail_id'
	LEFT JOIN $tablemeta as thumb_link ON `thumb_link`.`post_id` = `thumb_id`.`meta_value` AND `thumb_link`.`meta_key` = '_wp_attached_file'
	WHERE 	`post`.`post_type` = 'post' AND `post`.`post_status` = 'publish' 
	GROUP BY `ID`
	ORDER BY `date` DESC
	LIMIT %d OFFSET %d";
	return $data = $wpdb->get_results( $wpdb->prepare($sql, $limit, $offset), ARRAY_A);
?>
</code></pre>
