Query Posts by Post Format With WP_Query

Quick Summary
- Filter WP_Query by format with a tax_query on the post_format taxonomy, using slugs such as post-format-audio and post-format-video.
- Adding operator NOT IN reverses the condition, and standard posts have no format term, so they appear in the results.
- The Format option only shows in the editor when the theme declares add_theme_support for post-formats.
WordPress stores post formats as terms of a built-in taxonomy called post_format, so you filter posts by format with a tax_query, just as you would with categories or tags. The two queries below list the posts that have the Audio or Video format, and then every post that has neither.
Place the code in the template file where the list should appear, such as a page template or home.php.
Show only Audio and Video posts
Format slugs always start with post-format-. Because terms holds two slugs, a post matches if it has either format. With a single condition the relation key has no effect; it’s only there so you can add more conditions. Combining a category and a tag condition shows it at work.
<?php
$paged = get_query_var( "paged" )?get_query_var( "paged" ) : 1;
$posts_per_page = -1;
$_p = new WP_Query(array(
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-audio',
'post-format-video'
)
)
)
));
while( $_p->have_posts() ):
$_p->the_post();
?>
//POST TITLE
<h3><a href="<?php the_permalink( ); ?>">
<?php the_title( ); ?>
</a>
</h3>
<?php
endwhile;
wp_reset_query();
?>Exclude Audio and Video posts
The only change is 'operator' => "NOT IN", which reverses the condition. Standard posts have no format term at all, so they are included in this list.
<?php
$paged = get_query_var( "paged" )?get_query_var( "paged" ) : 1;
$posts_per_page = -1;
$_p = new WP_Query(array(
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-audio',
'post-format-video'
),
'operator'=>"NOT IN"
)
)
));
while( $_p->have_posts() ):
$_p->the_post();
?>
//POST TITLE
<h3><a href="<?php the_permalink( ); ?>">
<?php the_title( ); ?>
</a>
</h3>
<?php
endwhile;
wp_reset_query();
?>- The other format slugs are
post-format-aside,post-format-gallery,post-format-link,post-format-image,post-format-quote,post-format-statusandpost-format-chat. - The Format option only appears in the post editor if your theme declares support with
add_theme_support( 'post-formats', … ). posts_per_pageis-1, so every matching post is shown andpagedhas no effect.- Remove the
//POST TITLEline: it’s outside PHP and prints as text.
Related articles
- querying posts by a custom taxonomy term ID: the same tax_query, matched by term ID
- filtering posts by custom field values: meta_query works like tax_query for custom fields

