Menu Close

Exclude Post Formats From WordPress Custom Loop


I’ve recently released a new Premium WordPress Photography Theme and while setting up the blog section I added several different post formats – quotes, links, video, images and standard. This was great for showcasing various content on the blog, however, I didn’t want to have any actual posts for the quotes/link port formats because they have such little content, so I decided to remove any permalink structure for these.

The issue became when I was setting up a custom archives template, because I didn’t want any of the quote or link posts to show up since they don’t have any content. So after some messing around I came up with a great way to exclude any post formats from your custom WordPress loops by using a tax_query within my get_posts argument to exclude these post formats.

Below is a quick sample loop of how to exclude post formats using the tax_query.

$args = array(
 'numberposts' => 10,
 post_type' =>'post',
 'tax_query' => array(
 'taxonomy' => 'post_format',
 'field' => 'slug',
 'terms' => array( 'post-format-quote','post-format-link' ),
 'operator' => 'NOT IN',
 $posts= get_posts($args);

How the Tax Query Works?

We are basically using the tax query to show any posts that are  “Not In” the terms array which consist of quote and link post formats. Of course you can add in any post format you wish to exclude just like I have done by separating them with comas.  Checkout the post on about “Taxonomy Parameters” for further reading and usage.

View Source
Posted in WordPress