Help altering a query to exclude all but standard post format

The question:

I feel like I’ve been banging my head against the keyboard for a week with this problem. I’m trying to change my current query on my home page to show only posts that are set as standard posts using the new post format.

I’ve looked everywhere for answers (including here and here) and tried everything I’ve found, but I can’t get it to work within the framework of my current query.

My query is provided below. Any help is greatly appreciated.

<?php     
query_posts( array( 'post__not_in' => $ids, 'showposts' => 10, 'cat' => '-4' ) ); ?>
<?php while (have_posts()) : the_post(); ?>
-LOOP STUFF-
  <?php the_excerpt(); ?><BR>
<?php endwhile; ?>

UPDATE – March 30: Since I posted this I found out how to properly query for, we’ll say, image posts using this code:

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

But I still need to figure out how to do the opposite and only pull standard posts.

Second Update – March 30: I found the answer after some more hunting. Apparently the only way to pull the standard posts is to add:

‘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 so it won’t return any of those formats. Odd, but it works.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Okay, I found the answer after some more hunting. Apparently the only way to pull the standard posts is to add:

'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 so it won’t return any of those formats. Odd, but it works.

<?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',
      'operator' => 'NOT IN'
    )
  )
);  
    query_posts( $args ); ?>
<!-- The Loop -->
    <?php endwhile; ?>


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

Leave a Comment