Title: Getting posts from a specific post_format only
Last modified: August 19, 2016

---

# Getting posts from a specific post_format only

 *  Resolved [Gatto Nero](https://wordpress.org/support/users/gattonero/)
 * (@gattonero)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/getting-posts-from-a-specific-post_format-only/)
 * Hi all 🙂
 * I’ll try and be short (without being rude, I hope): I’m doing some heavy change
   to a wordpress theme – like uhm, doing it from scratch – and I’m really amazed
   at the potential of post_formats.
 * What I’d like to do is getting *only* posts from a specific post_format (ie.:
   gallery) and add them – ok, just a thumbnail 😉 – to my sidebar.
 * I tried with get_posts, but I have no idea which parameter should I use to get
   them, so… Documentation seems not to mention post_format, in these cases…
 * Any Idea?
 * I tried something like
 *     ```
       <?php
       $args = array( 'numberposts' => 3, 'post_format' => gallery );
       $lastposts = get_posts( $args );
       foreach($lastposts as $post) : setup_postdata($post); ?>
       	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
       <?php endforeach; ?>
       ```
   
 * Obviously, it doesn’t work 🙂
    I suppose post formats are a special type of post_type,
   but… which is the “name” of them?

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [Gatto Nero](https://wordpress.org/support/users/gattonero/)
 * (@gattonero)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/getting-posts-from-a-specific-post_format-only/#post-1964676)
 * I’ll add something… funny 🙂
    I tried to create a new category, called “gallery”,
   without adding a specific slug. Wordpress automagically created the slug “post-
   format-gallery”.
 * (Obviously, I’m not able to make get_posts function with category_name, but…)
 *  Thread Starter [Gatto Nero](https://wordpress.org/support/users/gattonero/)
 * (@gattonero)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/getting-posts-from-a-specific-post_format-only/#post-1964682)
 * Somehow, I did it.
 * I wrote the code here, in case someone needed it 🙂
 *     ```
       <?php
       $galleryposts = get_posts('category_name=post-format-gallery&post_count=4');
       foreach($galleryposts as $post) : setup_postdata($post); ?>
       	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
       <?php endforeach; ?>
       ```
   
 * What I’ve learnt:
    - The sample codes on the Codex don’t work always
    - get_posts seems to work perfectly as wp_query… but don’t use arrays 🙂
    - post-formats create a special category name called post-format-NAMEOFTHEFORMAT
 *  [Marian Heddesheimer](https://wordpress.org/support/users/heddesheimer/)
 * (@heddesheimer)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/getting-posts-from-a-specific-post_format-only/#post-1965091)
 * Hi Gatto,
 * thanks for posting your solution here. It pointed me to the right direction. 
   Anyway, your solution did not work for me, so it may be possible that it has 
   been changed in the latest WP version. With WP 3.3.1 I can now do this:
 *     ```
       query_posts(array('post_format' => 'post-format-gallery',
               'category_name' => 'Photo Contest'));
       ```
   
 * which works fine to get all posts from post format “Gallery” and category name“
   Photo Contest”.
 *  [blindscientist](https://wordpress.org/support/users/blindscientist/)
 * (@blindscientist)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/getting-posts-from-a-specific-post_format-only/#post-1965093)
 * Hello All,
    Am I understanding this correctly? I need to have category-based 
   blogroll on each of several different pages (based on the page’s category). For
   example, the SPORTS page would have a blog-style listing of each post with the
   SPORTS category assigned to it (several writers working on one magazine site –
   each writing within his/her category, i.e., fashion, dining, etc). Does this 
   code do that? If so, where? I know HTML and CSS but just learning php, etc. new
   to WP, so please excuse “noobness” of the question and point me in the right 
   direction. Thanks in advance,

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Getting posts from a specific post_format only’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [post_format](https://wordpress.org/support/topic-tag/post_format/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 4 replies
 * 3 participants
 * Last reply from: [blindscientist](https://wordpress.org/support/users/blindscientist/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/getting-posts-from-a-specific-post_format-only/#post-1965093)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
