Query by Post Format in WordPress

Follow @tristarweb

If you’re an avid WordPress developer like myself, you may have had a play about with the new “post formats” in WordPress 3.1. Great! What an awesome idea – now, how do I show only “standard” posts? Anyone..?

Yeah, it seemed almost impossible, but not quite! To query your WordPress posts by the standard post format only, you need to use the tax_query parameter, in conjunction with the NOT IN operator.

<?php
        $args = array(
        'showposts' => 9,
        'tax_query' => array(
                        array( 'taxonomy' => 'post_format',
                                  'field' => 'slug',
                                  'terms' => array('post-format-video','post-format-quote'),
                                  'operator' => 'NOT IN'
                                  )
                        )
        );
        query_posts( $args );
        while ( have_posts() ) : the_post(); ?>

                <?php the_title(); ?>

<?php endwhile; wp_reset_query(); ?>

As you can see, I am using query_posts to define what posts I would like to bring back. I have told it to filter by the taxonomy of post_format, and I have listed an array of the post_formats available on my WordPress site. I have then set the operator to NOT IN; this means it will bring back any posts that are not in the above formats – which in my case is the remaining standard posts!

I hope this helps someone out, as it was hard to find much information on it. They should really make it a bit simpler to bring back the standard post format.

About Gav Hoffman

Hi, I'm Gav and I try to make sure I am living life as much as possible and loving every minute of it. Designing and developing on a daily basis And lover of all things creative.

2 Responses to Query by Post Format in WordPress

  1. Shona says:

    20:23 02/04/2011

    Lovely write, good blog design, keep up the great work

  2. Reid says:

    17:57 01/07/2011

    Thanks a ton – this helped me out of pinch.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source code with [code][/code] tags.

Follow Tristar on Twitter