Truy vấn sql database wordpress get 10 bài viết theo chuyên mục

<?php
	global $wpdb;
	$table 	= $wpdb->prefix . 'posts';
	$tablemeta 	= $wpdb->prefix . 'postmeta';
	$tableRelationships = $wpdb->prefix . 'term_relationships';
	$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'
	LEFT JOIN $tableRelationships as relation ON `relation`.`object_id` = `post`.`ID`
	WHERE 	`post`.`post_type` = 'post' AND
		`post`.`post_status` = 'publish' AND
		`relation`.`term_taxonomy_id` = %d
	GROUP BY `ID`
	ORDER BY `date` DESC
	LIMIT %d OFFSET %d";
	return $data = $wpdb->get_results( $wpdb->prepare($sql, $cat_id, $limit, $offset), ARRAY_A);
?>

Last updated