WordPress query exclude post format

I can’t get this example here to work!http://www.snilesh.com/resources/wordpress/wordpress-query_post-exclude-or-include-post-formats/

I’m trying to get any latest post that isn’t a “Video” post format. I’m not sure how I actually write the query once I’ve set the arguments up, at least I can’t get it to work. Here’s my code:

Read More
<?php
$args = array(
  'tax_query' => array(
   array(
  'showposts' => '1', 
      'taxonomy' => 'post_format',
      'field' => 'slug',
      'terms' => 'post-format-video',
      'operator' => 'NOT IN'
    )
  )
);
query_posts( $args );
?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<!--latest news-->  
<div class="latestNews">
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php if ( has_post_thumbnail()) : ?>           
    <a class="thumbnail" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
        <?php the_post_thumbnail(); ?>
    </a>
 <?php endif; ?>
<?php the_excerpt(); ?>
<a href="?cat=1">more entries</a>
<a href="<?php the_permalink() ?>">read more</a>
</div>
<?php endwhile; ?>

I’m not sure if anyone can spot what I’ve done wrong there?! Would love any help possible!

Thanks

Related posts

Leave a Reply

2 comments

  1. You are using

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    

    But you didn’t asign the object in the variable, it should be

    $my_query = query_posts( $args );
    

    But, I think it’s better to use

    $my_query = new WP_Query( $args );
    

    So, you can use

    $args = array(
        'post_type' => 'post', // if the post type is post 
        'posts_per_page' => 1,
        'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => 'post-format-video',
            'operator' => 'NOT IN'
        ))
    );
    $my_query = new WP_Query( $args );
    
  2. Solution:

    $args = array(
        'tax_query' => array(
         array(
           'taxonomy' => 'post_format',
           'field' => 'slug',
           'terms' => array('post-format-video'),
           'operator' => 'NOT IN'
        ) )
    );
    get_posts( $args );