0

I am using the following code for a custom archive of all posts:

$taxonomy = 'category';
$param_type = 'category__in';
$term_args = array(
    'orderby' => 'slug',
    'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
    foreach( $terms as $term ) {
        $args = array(
            "$param_type" => array($term->term_id),
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {  ?>
            <h3 style="margin-bottom: 0px;"><?php echo $term->name;?></h3>
            <?php
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
                <ul style="list-style-type: circle;">
                <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
                </ul>
            <?php 
            endwhile;
        }
    }
}
wp_reset_query();  // Restore global post data stomped by the_post().

Now this pulls all posts, but is there a way to limit the results to only a certain post format?

Same for this:

echo $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts 
    WHERE post_type = 'post' AND post_status = 'publish'");
1
  • Welcome to WPSE. If you haven't done so, please take your time and visit our tour page. I just want to make sure, your looking to just retrieve posts from a specific post format under each term Sep 13 '14 at 7:06
1

Post formats are actually terms of the taxonomy post_format. I have done a post about this what taxonomies are and their hierarchies which you can check out here

To get a list of all terms under the taxonomy post_format, simply use get_terms and var_dump() the result

$terms = get_terms('post_format','hide_empty=0');
  ?><pre><?php var_dump($terms); ?></pre><?php  

This will print

array(5) {
  [0]=>
  object(stdClass)#207 (9) {
    ["term_id"]=>
    string(3) "142"
    ["name"]=>
    string(5) "Aside"
    ["slug"]=>
    string(17) "post-format-aside"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "142"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
  [1]=>
  object(stdClass)#310 (9) {
    ["term_id"]=>
    string(3) "129"
    ["name"]=>
    string(5) "Audio"
    ["slug"]=>
    string(17) "post-format-audio"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "129"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
  [2]=>
  object(stdClass)#309 (9) {
    ["term_id"]=>
    string(3) "105"
    ["name"]=>
    string(7) "Gallery"
    ["slug"]=>
    string(19) "post-format-gallery"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "105"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
  [3]=>
  object(stdClass)#308 (9) {
    ["term_id"]=>
    string(3) "128"
    ["name"]=>
    string(5) "Quote"
    ["slug"]=>
    string(17) "post-format-quote"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "128"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
  [4]=>
  object(stdClass)#307 (9) {
    ["term_id"]=>
    string(3) "106"
    ["name"]=>
    string(5) "Video"
    ["slug"]=>
    string(17) "post-format-video"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "106"
    ["taxonomy"]=>
    string(11) "post_format"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "0"
  }
}

One thing though that I want to highlight, standard is not a post format, which is a let down. Any post without a designated post format should actually be assigned a post format default. The problem is, if you need only posts that don't have a special post format, you need to run a tax_query and exclude all posts that does have a post format.

So, to get posts from a certain post format, you can run a tax_query with WP_Query

Example to get all posts from video post format:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => 'post-format-video',
        ),
    ),
);

$query = new WP_Query( $args );

If you need to get posts without any post format attached, you'll need to do something like

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => array('post-format-video',' post-format-quote', 'post-format-gallery', 'post-format-audio', 'post-format-aside' ),
            'operator' => 'NOT IN',
        ),
    ),
);

$query = new WP_Query( $args );
0

Add a taxonomy argument to specify the post type in your WP_Query. Here's an example straight from codex:

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

You need to replace post-format-quote with the one you need.

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.