您必须查询未附加帖子格式的帖子:
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'operator\' => \'NOT IN\',
\'terms\' => get_terms(\'post_format\')
)
)
);
为了提高性能,您可以对post格式进行硬编码,而不是使用
get_terms
要检索它们,请执行以下操作:
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'operator\' => \'NOT IN\',
\'terms\' => array(\'post_format-aside\',\'post_format-chat\',\'post_format-gallery\',\'post_format-link\',\'post_format-image\',\'post_format-quote\',\'post_format-status\',\'post_format-video\',\'post_format-audio\')
)
)
);
<小时>
Edit
一旦您在注释中请求它,我将为您提供可以执行相同任务的原始SQL查询。请注意,一旦没有post格式的post没有存储在任何地方(标准post格式意味着没有post格式),要获得所需的内容,您需要嵌套2个查询,其中嵌套的查询包含3个表之间的联接。这远远不是所谓的性能查询。为此,最好将结果缓存在瞬态中:
function post_without_formats() {
$cached = get_transient(\'post_without_formats\');
if ( $cached ) return $cached;
global $wpdb;
$post_without_format = $wpdb->get_results(
"SELECT * FROM $wpdb->posts WHERE post_status = \'publish\'
AND post_type = \'post\' AND ID NOT IN (
SELECT p.ID FROM $wpdb->posts as p
LEFT JOIN $wpdb->term_relationships as tr ON tr.object_id = p.ID
LEFT JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE p.post_status = \'publish\' AND p.post_type = \'post\'
AND tt.taxonomy = \'post_format\'
GROUP BY p.ID
)"
);
set_transient(\'post_without_formats\', $post_without_format);
return $post_without_format;
}
function reset_post_without_formats($term, $taxonomy) {
if ( $taxonomy == \'post_format\' ) {
delete_transient(\'post_without_formats\');
post_without_formats();
}
}
add_action(\'edited_term_taxonomy\', \'reset_post_without_formats\', 99, 2);
正如您所看到的,每次在帖子中添加或删除帖子格式时,我都会重置瞬态。这将使后端速度降低一点,但会提高前端速度。