Justin
Justin

Reputation: 329

Only show WordPress 'standard' post format on template?

I recently added post formats to my WordPress theme - on the blog page its fine as they are all styled accordingly. However on my home page template I only want to show 'standard' posts formats (no links, galleries, audio, video etc.).

In my theme options I can decide how many posts to display on the front page which is what 'dft_recent_number' is for.

Does anybody know how I can change the code below to exclude all but 'standard' post formats?

<?php 
$query = new WP_Query();
    $query->query('posts_per_page='.get_option('dft_recent_number'));

    //Get the total amount of posts
$post_count = $query->post_count;

    while ($query->have_posts()) : $query->the_post(); 

 ?>

Any help is much appreciated!

Upvotes: 2

Views: 5551

Answers (3)

Andrea Sciamanna
Andrea Sciamanna

Reputation: 1474

I know that's old, but I was facing the same issue and even though I've found a solution I wondered what other have done to "fix" that.

I think a more scalable solution could be something like that:

$post_formats = get_theme_support( 'post-formats' );

$tax_query = false;
if ( $post_formats ) {
    $tax_query = array(
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => $post_formats[0],
            'operator' => 'NOT IN'
        )
    );
}

// WP_Query arguments
$args = array(
    'post_type' => 'post',
    'order'     => 'DESC',
    'orderby'   => 'date',
    'tax_query' => $tax_query
);

This would exclude the post formats that have been enabled and also will work in case WP will add more post formats (or add the ability to add more).

Upvotes: 1

Rahman Saleh
Rahman Saleh

Reputation: 1108

// there's no post-format-standard so you should write it like this to exclude all other postpformats
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-quote','post-format-audio','post-format-gallery','post-format-image','post-format-link','post-format-video'),
'operator' => 'NOT IN'
)

Upvotes: 4

Jason McCreary
Jason McCreary

Reputation: 72961

WP_Query does not seem to have a straightforward parameter for post_format.

From my quick research it appears that post formats are related through taxonomies. So, theoretically, using the taxonomy parameters should work.

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => 'post-format-standard',
        )
    )
);
$query = new WP_Query( $args );

Note: you'll need to update the taxonomy names and slugs for your blog. This should be the names you set in your functions.php file.

Upvotes: 4

Related Questions