• Hello guys, I am trying to query by post format. Here is my code,

    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'post-format',
                'field' => 'slug',
                'terms' => array('post-format-video'),
            )
        )
    );
    
    $query = new WP_Query($args);
    
    if ($query -> have_posts()) : while ($query -> have_posts()) : $query -> the_post();
    
    $out .= get_the_title();
    
    endwhile; endif;
    
    return $out;

    The query works fine when I do this,

    $query = new WP_Query('showposts=5');

    It just does not return anything when I try to use the ‘tax_query’ as shown above.

    Also, I am using the WooTumblog plugin which is registering the formats with this line, and I can see the post formats in my admin so there should not be a problem there.

    add_theme_support( 'post-formats', array( 'aside', 'image', 'audio', 'video', 'quote', 'link' ) );

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Yeowza

    (@yeowza)

    Anyone use post formats? ??

    There don’t seem to be a lot of experts yet for any of this. I’ve been trying to find an answer to a question about querying post formats myself, but I’m in the same boat as you.

    Thread Starter Yeowza

    (@yeowza)

    Thanks for the reply GATEKeeper.

    I’ve found a few topics on it, https://wordpress.stackexchange.com/questions/10855/how-do-i-query-by-post-format-in-wordpress-3-1

    But it does not seem to work for anyone. Maybe we’ll have a solution in the next update for this.

    Interesting. The final code they posted does work for me – the key thing to note is that it’s post_format, not post-format:

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

    But I still can’t get that to work within the context of my query:

    <?php
    	query_posts( array( 'post__not_in' => $ids, 'showposts' => 10, 'cat' => '-4' ) ); ?>
        <?php while (have_posts()) : the_post(); ?>
    <!-- Loop Stuff -->
        <?php endwhile; ?>

    Wait, never mind. I solved it now that I had the right code.

    I still need to test it more, but this should work in my specific context:

    <?php
    $args = array(
      'post__not_in' => $ids,
      'showposts' => 10,
      'cat' => '-4,-1866,-27',
      'tax_query' => array(
        array(
          'taxonomy' => 'post_format',
          'field' => 'slug',
          'terms' => 'post-format-image'
        )
      )
    );
    	query_posts( $args ); ?>
    <!-- Look Stuff -->
        <?php endwhile; ?>

    My final, remaining question though is how I query for standard post formats? post-format-standard does not work.

    Eureka, I found the final piece of this.

    The key for me is to add at the end:

    'operator' => 'NOT IN',

    so it looks for posts that aren’t in the image post format. Or I have to add an array of terms to the terms line.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Post Formats’ is closed to new replies.