WordPress.org

Ready to get started?Download WordPress

Forums

Post Formats (7 posts)

  1. Yeowza
    Member
    Posted 3 years ago #

    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' ) );

  2. Yeowza
    Member
    Posted 3 years ago #

    Anyone use post formats? :)

  3. GATEKeeper
    Member
    Posted 3 years ago #

    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.

  4. Yeowza
    Member
    Posted 3 years ago #

    Thanks for the reply GATEKeeper.

    I've found a few topics on it, http://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.

  5. GATEKeeper
    Member
    Posted 3 years ago #

    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; ?>
  6. GATEKeeper
    Member
    Posted 3 years ago #

    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.

  7. GATEKeeper
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.