Only get_posts of certain post formats

You can’t actually pass a taxonomy-related argument to get_posts(). (Edit: actually, yes you can. The Codex is just somewhat unclear. Looking at source, get_posts() is, at its heart, just a wrapper for WP_Query().) You can pass meta keys/values, and post types, but not taxonomies such as post format. So for this line:

$myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');

I would recommend using WP_Query() rather than get_posts():

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

Note: yes, that’s a lot of nested arrays. Tax queries can be tricky like that.

The next step is to modify your loop open/close statements. Change these:

<?php foreach($myposts as $post) : ?>

    <?php /* loop markup goes here */ ?>

<?php endforeach; ?>

…to this:

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

    <?php /* loop markup goes here */ ?>

<?php endwhile; endif; ?>

<?php wp_reset_postdata(); ?>

Your actual loop markup should be able to remain the same, except that you no longer need to call setup_postdata( $post ):

<?php        
    $year = mysql2date('Y', $post->post_date);
    $month = mysql2date('n', $post->post_date);
    $day = mysql2date('j', $post->post_date);    
?>

<p>
    <span class="the_article">
        <a href="https://wordpress.stackexchange.com/questions/48901/<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </span>
    &nbsp;&nbsp;&nbsp;
    <span class="the_day">
        <?php the_time('j F Y'); ?>
    </span>
</p>

So, putting it all together:

<?php
// Only query posts with the
// "standard" post format, which
// requires *excluding* all other
// post formats, since neither the
// "post_format" taxonomy nor the
// "post-format-standard" taxonomy term
// is applied to posts without
// defined post formats
$myposts = new WP_Query( array(
    'tax_query' => array(
        array(                
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 
                'post-format-aside',
                'post-format-audio',
                'post-format-chat',
                'post-format-gallery',
                'post-format-image',
                'post-format-link',
                'post-format-quote',
                'post-format-status',
                'post-format-video'
            ),
            'operator' => 'NOT IN'
        )
    )
) );

// Open the loop
if ( $myposts->have_posts() ) : while ( $myposts->have_posts() ) : $myposts->the_post(); ?>

    $year = mysql2date('Y', $post->post_date);
    $month = mysql2date('n', $post->post_date);
    $day = mysql2date('j', $post->post_date);    
    ?>

    <p>
        <span class="the_article">
            <a href="https://wordpress.stackexchange.com/questions/48901/<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </span>
        &nbsp;&nbsp;&nbsp;
        <span class="the_day">
            <?php the_time('j F Y'); ?>
        </span>
    </p>  
    <?php 

// Close the loop
endwhile; endif;

// Reset $post data to default query
wp_reset_postdata();

Leave a Comment