Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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.

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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();
share|improve this answer
    
thanks, you made it really easy for a beginner to understand by breaking it down nicely. I'm guessing since I only use aside, link and standard post formats, I can actually skip the rest. –  daba Apr 13 '12 at 21:37
    
Yes; you would only need to include whichever post formats you've enabled support for. –  Chip Bennett Apr 13 '12 at 21:43
    
get_posts() actually utilises WP_Query, so of course you can pass in taxonomy queries, just pass them as an array and not as a query string. –  shabushabu Apr 14 '12 at 10:45
    
@shabushabu thanks for that. I've updated my answer. –  Chip Bennett Apr 14 '12 at 15:00

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}.

share|improve this answer
    
thanks, but I'm trying to create the archives within a page template, so I'll go with one of the other solutions :) –  daba Apr 13 '12 at 21:35

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'
        )
    )
);
share|improve this answer

You can do trick like that:

<?php 
while( have_posts() ) : the_post();
get_post_format()==false? get_template_part( 'loop', 'posts' ) : false;
endwhile;
?>

It is because get_post_format() for standard post format returns false. http://codex.wordpress.org/Function_Reference/get_post_format

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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