你为什么不使用无脂肪的$wpdb->get_var() 对于任务?
global $wpdb;
$ID = $wpdb->get_var("SELECT `ID` FROM {$wpdb->posts}
WHERE `post_type`=\'post\' AND `post_status`=\'publish\'
ORDER BY `post_date_gmt` DESC LIMIT 1;");
$post = $ID ? get_post($ID) : null; // Now get the actual post
这是一种更高效、更轻的方式。没有get\\u posts()或WP\\u Query()的所有提示。
当做
UPDATE: (更多内容)
function get_last_published_post(){
global $wpdb;
// Get the ID of the last published post (by date)
$ID = $wpdb->get_var("SELECT `ID` FROM {$wpdb->posts}
WHERE `post_type`=\'post\' AND `post_status`=\'publish\'
ORDER BY `post_date_gmt` DESC LIMIT 1;");
return $ID ? get_post($ID) : null; // Now get the actual post
}
// Should print out the Post object
var_dump(get_last_published_post());