WordPress Include Or Exclude Post Formats



Wordpress Development

Hi Friends, As I explained in my last posts that WordPress have 9 post formats and how you can use them in your theme. Now sometimes we have requirements that we have 9 post types but need to show post for only selected post formats. So in this article I am explaining you how you can get post from particular post formats and how you can exclude posts of particular formats.

Now for example if you want to show or get posts of video post format. So here is code for it :

$videos = get_posts( array(
    'tax_query' => array(
        array(
          'taxonomy' => 'post_format',
          'field'    => 'slug',
          'terms'    => array( 'post-format-video' ),
          'operator' => 'IN'
        )
    )
) );

In above code you can see we use tax_query. We use operator IN and in terms ‘post-format-video’, it means it will return all posts which have video post format. Now you can show them by using loop.

So as you see ‘post-format-video’ for terms, you can pass any of other post format like ‘post-format-{post format name}’, So all post format term will be :

  • post-format-aside
  • post-format-audio
  • post-format-chat
  • post-format-gallery
  • post-format-image
  • post-format-link
  • post-format-status
  • post-format-quote
  • post-format-video

Now how to get posts by excluding particular post formats, For example if we don’t want to get post which have post format image and link, So we have to use operator ‘NOT IN’ instead of ‘IN’ and in terms use ‘post-format-link’ and ‘post-format-image’, So finally code would be look like this to exclude posts of link and image post format.

$videos = get_posts( array(
    'tax_query' => array(
        array(
          'taxonomy' => 'post_format',
          'field'    => 'slug',
          'terms'    => array( 'post-format-link', 'post-format-image' ),
          'operator' => 'NOT IN'
        )
    )
) );

So this is way to include and exclude post formats and get posts.I hope this article helps you.If you really like this article tweet about it or share it on Facebook or make comment.

  • http://www.webhostings.in/ web hosting in india

    I follow above the steps to include wordpress plugin in my project.I get it clear information through this blog.

  • web design services

    hey ankur, your blog gives valuable information . In previous blog, you had explain very well about the how to post formats in wordpress. Now, it’s blog are very useful to all of wordpress users. With the help of this blog, we can learn except post of paricular formats.

  • Nitesh

    Thank for sharing this information,You had explain very well & easy language