Querying By Post Format
tax_query
argument for WP_Query
. To query for all posts with the quote format, we would create our WP_Query
like this:$args = array( 'post_type' => 'post', 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-quote', ) ) ); $query = new WP_Query( $args );
Note that in the above example, the term is prefixed with "post-format-". Also keep in mind that we can pass an array of terms to the terms argument in the tax_query
in order to query by more than one format.
Making A Post Format Menu
If you have lots of posts on your site in a few post formats you probably want to highlight each format. For example, if you are a visual artist using your blog to show off your videos and images, and are using post formats, you can create a menu with your posts of the format video and image separately.
The first step is to set up an array of arguments for WP_Query
, just like the one I showed you above, except this time without the specific post format specified in the "terms" argument of the tax_query
and an array of formats, along with labels to use in the menu output.
Here is what these two arrays look like:
// initial WP_Query args $args = array( 'post_type' => 'post', 'posts_per_page' => 5, 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', ) ) ); // formats to show $formats = array( array( 'name' => 'video', 'label' => 'Videos' ), array( 'name' => 'image', 'label' => 'Photos' ), );
With these two arrays, we can create a foreach
loop out of the second array, to query for posts with the current format and outputting a list of the posts. This will involve, in each iteration of the loop, adding the terms argument using set()
, creating a instance of WP_Query
, looping through it, and then unsetting the terms argument.
Inside of the WP_Query
while loop we can use get_term_link()
to create a link to the post format's archive page, which is really a taxonomy term archive. For the individual posts, we can use get_permalink()
to create the link to the post. Here is what the loop looks like:
echo '<nav class="menu">'; echo '<ul>'; foreach ( $formats as $format ) { //convert name key to the proper name for the term $term_name = 'post-format-'.$format[ 'name' ]; //add a terms arg to the tax_query's args $args[ 'tax_query' ][ 0 ][ 'terms' ] = $term_name; $query = new WP_Query( $args ); if ( $query->have_posts() ) { echo '<li>'.get_term_link( $term_name, 'post_format' ).'</li>'; echo '<ul class="'.$term_name.'-submenu">'; while ( $query->have_posts() ) { echo '<li><a href="'.get_permalink( $query->post->ID ).'" />'.get_the_title( $query->post->ID ).'</a></li>'; } //endwhile wp_reset_postdata(); echo '</ul></li>'; } //endif //before moving to next itteration remove the term from the WP_Query args unset( $args[ 'tax_query' ][ 0 ][ 'terms' ] ); } //endforeach echo '</ul></nav>';
Be sure to add any additional classes or IDs to the markup to make this generic code look like a menu in your theme and feel free to substitute the formats that are being shown according to your needs.
Removing Post Formats From the Main Blog Index
Now that you have a menu for your post formats, you may want to exclude posts of certain formats from your main blog index. This will allow you to use your blog as a blog and the post format menu to highlight videos, images, audio files or other formatted posts.
In order to keep posts of a specific format, you will need to use a pre_get_post
filter to add a tax_query
to the main query. The tax_query
will look similar to the ones I already showed you, but will have an extra argument to exclude posts with the format, using the "operator" argument. By setting operator to "NOT IN" any matching posts will be excluded.
This query will also be wrapped in the conditional is_home()
to ensure it only affects the main blog index.
Here is what the action and callback function look like:
add_action( 'pre_get_posts', 'slug_no_videos_home' ); function slug_no_videos_home( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $tax_query = array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video', 'operator' => 'NOT IN', ) ); $query->set( 'tax_query', $tax_query ); } return $query; }
As you can see in the callback, we define a tax_query
, using the NOT IN
operator and then use the set method to add the tax_query
to the main query of the blog index.
Go Forth and Format
Through out this five-part series, you have learned what post formats are, how to add support for them to your theme, how to use them in the loop, target them with custom CSS, bulk update them, and use them as content organizing tool.
Armed with this knowledge, you can create mor e powerful themes that make data portability a breeze.
Comments