嗯,您的代码有很多地方不完全正确:
$posts = $wpdb->get_results( "SELECT ID, post_title, post_modified_gmt
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships as t
ON ID = t.object_id
WHERE post_type = \'post\' AND post_status = \'publish\' AND t.term_taxonomy_id = 3
WHERE post_status = \'publish\' # <-- !! second WHERE clause
AND post_password = \'\'
AND post_type = \'post\' # <-- !! you\'ve already used this condition
ORDER BY post_type DESC, post_modified DESC # <-- !! why do you order by post_type if you select only posts??
LIMIT 10"
);
那么如何更好地书写呢
global $wpdb;
$posts = $wpdb->get_results( $wpdb->prepare(
"SELECT p.ID, p.post_title, p.post_modified_gmt
FROM {$wpdb->posts} as p
INNER JOIN {$wpdb->term_relationships} as t # <-- you can use INNER JOIN in here, since you want posts from category, so every post has to have some term assigned to it
ON (p.ID = t.object_id)
WHERE p.post_type = \'post\' AND p.post_status = \'publish\' AND p.post_password = \'\' AND t.term_taxonomy_id = %d
ORDER BY p.post_modified_gmt DESC
LIMIT 10",
3 // <-- Notice: you\'ll have to use term_taxonomy_id and not term_id in here
));