I have looked in the WP codex for an example which shows how to use a taxonomy query to retrieve a set of posts that have no post_format.
IOW: ‘All posts where an aside or quote or link…etc… post_format has not been assigned; eg. a ‘standard’ post_format.’
But I can’t seem to figure out how to query for posts that have -no- post_format.
IOW: it seems as though if the post has no post_format (eg. ‘aside’ or ‘quote’) it returns an empty result in get_post_format()
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(?????)
)
)
);
$query = new WP_Query( $args );
How does one set up this kind of query?
You have to query post that has no post format attached:
To improve performance you can hardcoding the post format instead of using
get_terms
to retrieve them:Edit
Once you ask for it in comments, I’ll give you the raw SQL query that can perform same task. Please note that once post without post format are not stored anywhere (standard post format means no post format), to obtain what you want you need to nest 2 queries with the nested containing a join among 3 tables.
This is very far from what you can call a performant query. For this it’s a good idea cache the result in a transient:
As you can see I reset the transient everytime a post format is added or removed from a post. This will slow down a bit your backend but will increase the frontend speed.