How do I query by post format in WordPress 3.1

I am trying to query for all posts with a post format of ‘quote.’ I have added the post formats to my functions.php with

add_theme_support( 'post-formats', array( 'image', 'video', 'gallery', 'quote' ) );

I have selected ‘quote’ as the format for the post in the admin. The last example under Taxonomy_Parameters shows how to display posts that have the ‘quote’ format but when I run it in my theme no posts are returned. Here is the code:

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

When I just query all posts and place

echo get_post_format();

in the loop it returns the word ‘quote’ on the front-end. Also, when I var_dump() the query I do not see anything in the array about post format.

Does anyone know if it is possible to query by post format? If so how?

EDIT – See 5 comment under Bainternet’s answer:
This is the code found on index.php of the twentyten theme of a fresh install trying to return format type quotes. I return ‘no’ instead of ‘quote’. Can you see anything that I should change.

get_header(); ?>
<div id="container">
  <div id="content" role="main">
    <?php $args = array(
      'tax_query' => array(
        array(
          'taxonomy' => 'post-format',
          'field' => 'slug',
          'terms' => array('quote')
        )
      )
    );
    query_posts( $args );
    if ( have_posts() ) : while ( have_posts() ) : the_post();
      echo get_post_format();
    endwhile; else:
      echo 'no';
    endif;
    wp_reset_query();      
    ?>
  </div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

EDIT 2 – It appears that the WordPress Codex has now changed and the portion on Taxonomy Parameters is only found in Google cache.

EDIT 3 – FINAL WORKING CODE

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

The twenty-ten edit from the first edit will be…

get_header(); ?>
<div id="container">
  <div id="content" role="main">
    <?php $args = array(
      'tax_query' => array(
        array(
          'taxonomy' => 'post_format',
          'field'    => 'slug',
          'terms'    => 'post-format-quote'
        )
      )
    );
    query_posts( $args );
    if ( have_posts() ) : while ( have_posts() ) : the_post();
      the_title();
      echo get_post_format();
      echo '<br />';
    endwhile; else:
      echo 'no';
    endif;
    wp_reset_query();      
    ?>
  </div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This code is incorrect! You have

'taxonomy' => 'post-format'

But it really needs to be:

'taxonomy' => 'post_format'

Without the underscore, the query will be invalid. I just tested this on my WordPress 3.1 install after pulling my hair out for hours.

Hope that helps!!

Method 2

in tax_query “terms” accepts array so you need to put post-format-quote in an array like this:

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x