Only get_posts of certain post formats

I’m trying to create a archive list with only my “normal” post format articles (not link, aside, quote, etc formats).

How would I implement has_post_format( 'standard' ), or something similar, into the code below?

Read More

I haven’t been able to find a query for get_posts that only requests specific format types.

<?php    
    // Get the posts
    $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');     
?>

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

<?php    
    // Setup the post variables
    setup_postdata($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="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </span>
    &nbsp;&nbsp;&nbsp;
    <span class="the_day">
        <?php the_time('j F Y'); ?>
    </span>
</p>

<?php endforeach; ?>

My php skills are at the beginner level at best, so any help would be much appreciated.

Related posts

Leave a Reply

4 comments

  1. 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="<?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="<?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();
    
  2. Post formats are just predefined terms in a taxonomy called post_format, so you should be able to use the WP template hierarchy to create post format archives. Just create a file called taxonomy-post_format-post-format-standard.php in the root of your theme and that file will be used to output all your standard posts. You can substitute ‘standard’ with any of the other format names, like aside, link or video, so e.g. taxonomy-post_format-post-format-video.php. This works for any other taxonomy as well, btw, as long as you stick to this format: taxonomy-{TAXONOMY_NAME}-{TERM_NAME}.php

    If you want to show post formats with a custom loop, e.g. in your sidebar or within a page template, then you can use the tax query from @kaiser. Just substitute the taxonomy with post_format and the slugs with post-format-{FORMAT_NAME}.

  3. For two different taxonomies. For a single one, you can leave the relation arg out.

    $args = array(
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'movie_janner',
                'field' => 'slug',
                'terms' => array( 'action', 'commedy' ) // Single terms as string - multiple as array
            ),
            array(
                'taxonomy' => 'actor',
                'field' => 'id',
                'terms' => array( 103, 115, 206 ),
                'operator' => 'NOT IN'
            )
        )
    );