10

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(); ?>

2 Answers 2

7

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!!

2
  • Thank you very much for catching that. I could swear that I copied the original code directly from the Codex page. (Maybe it was wrong there as well at one time.) It works now. Along with changing it to post_format I also needed to change 'terms' => 'quote' back to 'terms' => 'post-format-quote'. For anyone else interested please see Edit 3 of the original post for the final code.
    – PNMG
    Commented Mar 30, 2011 at 14:42
  • Sure, glad I was able to help. :) Commented Mar 30, 2011 at 17:25
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 );
5
  • Has anyone tried this and gotten it to work. I added the array part and it still did not return anything. Is post-format-quote the correct slug to put in the array?
    – PNMG
    Commented Mar 1, 2011 at 22:53
  • no its not you need to put the post format it self like "quote" and not post-format-quote
    – Bainternet
    Commented Mar 1, 2011 at 23:24
  • Do you mean I should put 'terms' => array('quote') since that is what I have quote defined as up in the add_theme_support function? I tried that as well. Still no luck.
    – PNMG
    Commented Mar 1, 2011 at 23:30
  • well 'terms' => array('quote') is working form me, try to echo out the post format with 'echo get_post_format();' and see what you get
    – Bainternet
    Commented Mar 1, 2011 at 23:49
  • Ok, so when I echo that I get 'quote'. In a moment of desperation I have turned off all plugins and switched back to the default twentyten theme, and created 3 test posts, one with a format of quote. I have updated the functions.php file to add quote to the add_theme_support call and updated index.php to use the following code: [see edit at bottom of original post]
    – PNMG
    Commented Mar 2, 2011 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.