Skip to main content
Easily create better & faster websites with the Total WordPress Theme Learn More
(opens a new tab)
Tutorials

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(
 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 WordPress.org about “Taxonomy Parameters” for further reading and usage.

2 Comments
  1. HAMZA · 10 years ago

    hey so si have a tabs widget and i have recent posts section in it and i want to exclude the post format from it and i dont know what to do this is the code of my function

    • AJ Clarke · 10 years ago

      Your code didn’t go through well via the comments, send me a link to the code on a gist or pastebin and I’ll have a look.

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.