Getting The Most of Post Formats: Content Organization Tools

Querying By Post Format

I know what I said before, about post formats not being an organizational tool, but as long as you're marking your videos as videos, images as images, etc., you might as well use that to organize your site. Adding a redundant taxonomy doesn't make any sense. Especially since post formats are set using the taxonomy "post_format."

This means that we can use a similar query to the one we used in the previous step to query by taxonomy, this time using the "post_format" taxonomy to query by taxonomies. Again we will use the tax_query argument for WP_Query. To query for all posts with the quote format, we would create our WP_Query like this:

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:

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:

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:

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.

Tags:

Comments

Related Articles