2

So, I have been looking EVERYWHERE. I tried every kind of combination. So far, nothing. I have to query two posts to show on my front page, of post post-type, not in one category but, most importantly, of the standard post-format. I work with wp 3.9.1. This is the syntax I use:

$query = new WP_Query( array(
            'post_type' => 'post',
            'cat' => '-1',
            'posts_per_page' => 2,
            array(
                'taxonomy' => 'post_format',
                'field' => 'slug',
                'terms' => array( 'post-format-gallery', 'post-format-video' ),
                'operator' => 'NOT IN'
                )
        ) );

It just does not work. I get two results, but the video and gallery formats are not processed out. I tried also the use of tax_query => array (.....) with no result at all.

In the first case the $query->have_posts returns all posts of all formats. In the second case it returns no content at all.

6

It looks like your syntax is a little off for the tax query. Try this:

$query = new WP_Query( array(
        'post_type' => 'post',
        'cat' => '-1',
        'posts_per_page' => 2,
        'tax_query' => array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array('post-format-aside', 'post-format-gallery', 'post-format-link', 'post-format-image', 'post-format-quote', 'post-format-status', 'post-format-audio', 'post-format-chat', 'post-format-video'),
            'operator' => 'NOT IN'
           ) )
       );

(Aside: If you're not using some of those post formats, it's fine to remove them from the 'terms' array. But if the goal is to only display the 'normal' format, you might want to leave them all there in case of future content changes.)

  • Ah, beat me to it. This looks good to me. Tax query should do the job here. – Dan Jul 7 '14 at 12:48
6

Since WordPress 4.1, WP_Tax_Query supports the NOT EXISTS operator for taxonomy queries. As a post has the "Standard" format if and only if it has no specified format, this is now the preferred method to query posts with the Standard post format. It also covers the future/customization case where post formats may be added, removed and changed. The resulting taxonomy query is simply:

array(
    'taxonomy' => 'post_format',
    'operator' => 'NOT EXISTS',
)

Using it in the example query, we get:

$query = new WP_Query( array(
    'post_type' => 'post',
    'cat' => '-1',
    'posts_per_page' => 2,
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'operator' => 'NOT EXISTS',
        ),
    ),
);
  • This is the better, more correct approach. – Daniel Bachhuber May 2 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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