纯SQL问题是离题的,但除此之外,纯SQL是危险的,因为如果WordPress核心更改数据库结构,它可能会崩溃。
您最好尽可能使用核心工具。在这种情况下,WP_Query
可以做你需要的事。
$args = array(
\'post_type\' => \'post\',
\'meta_query\' => array(
array(
\'key\' => \'status\',
\'value\' => null,
\'compare\' => \'NOT EXISTS\'
)
)
);
$query = new WP_Query( $args );
将生成以下SQL:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = \'status\')
WHERE 1=1
AND wp_posts.post_type = \'post\'
AND (wp_posts.post_status = \'publish\')
AND (wp_postmeta.post_id IS NULL)
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC LIMIT 0,3