WordPress query_post Exclude or include Post Formats

WordPress Post Formats is a theme feature introduced in WordPress 3.1. Its added extra functions to theme author to customize their wordpress theme with new meta feature. Post formats provides standardized list of formats , new formats cannot be added through themes or even plugins.

In short, with a theme that supports Post Formats, a blogger can change how each post looks by choosing a Post Format from a radio-button list. Options are added in admin section to select post format when user add a new post.

WordPress Standard Post Formats

  1. aside
  2. gallery
  3. link
  4. image
  5. quote
  6. status
  7. video
  8. audio
  9. chat

WordPress Query to Display Post Of Specific Post Format

If you want to display posts of specific post format then add following lines of code just above the while loop (WordPress Loop).

<?php
$args = array(
  'tax_query' => array(
    array(
      'taxonomy' => 'post_format',
      'field' => 'slug',
      'terms' => 'post-format-ABC'  
    )
  )
);
query_posts( $args );
?>

Replace ABC with post format name like if i want to display posts of video post format then i will edit that code as

<?php
$args = array(
  'tax_query' => array(
    array(
      'taxonomy' => 'post_format',
      'field' => 'slug',
      'terms' => 'post-format-video'  
    )
  )
);
query_posts( $args );
?>

If you want to display posts from more than one post format then use following code.

<?php
$args = array(
  'tax_query' => array(
    array(
      'taxonomy' => 'post_format',
      'field' => 'slug',
      'terms' => array('post-format-video','post-format-quote')
    )
  )
);
query_posts( $args );
?>

WordPress Query to Exclude Post Of Specific Post Format

If you want to exclude posts of specific post format then add following lines of code just above the while loop (WordPress Loop)

<?php
$args = array(
  'tax_query' => array(
    array(
      'taxonomy' => 'post_format',
      'field' => 'slug',
      'terms' => 'post-format-ABC',
      'operator' => 'NOT IN'
    )
  )
);
query_posts( $args );
?>

Replace ABC with name of post format which you want to exclude.

To Exclude more than one post formats from wordpress query use following code

<?php
$args = array(
  'tax_query' => array(
    array(
      'taxonomy' => 'post_format',
      'field' => 'slug',
      'terms' => array('post-format-video','post-format-quote')
    )
  )
);
query_posts( $args );
?>

Leave a Reply

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