Advertisement
  1. Code
  2. Theme Development
Code

Getting The Most of Post Formats: Bulk Updating Post Formats

by
Length:ShortLanguages:

Now that I've convinced you of how cool post formats are, you might be worried about having to go back through all of your posts, opening each one and manually setting its post format.

With a site of any decent size that could be a nightmare. Instead, let me show you how to automate the process.

Introducing set_post_format

Instead of updating post formats manually, we can use set_post_format() to update arrays of posts. First thing you will need to do is find the IDs of the posts that need to go into each post format and create one array for each post format. By the way, this process will be a lot easier if you have the plugin simply show IDs installed.

Once you have the IDs in an array, in the debug console or anywhere else you can execute PHP, you can pass that array through set_post_format(), like this:

Setting Post Format by Taxonomy Term

If you're just now learning about post formats and have previously been using categories or taxonomies to organize your posts, then setting post formats may be even easier for you.

Instead of finding all of the post IDs to feed to set_post_format() manually like in the last code example, you can use WP_Query to find them for you.

If you're just now learning about post formats and have previously been using categories or taxonomies to organize your posts, then setting post formats may be even easier for you.

In the example below, I show you how to query for all posts with a custom taxonomy of 'post-type' that have the term 'videos' and assign them to the post format 'video'. This code works by finding all matching posts using WP_Query and looping through them. But instead of looping each post in order to display it, it updates the post format using set_post_format().

No Excuse

In the previous parts in this series, I've dispelled common misunderstandings about post formats and showed you how to enable support for them, and use them in your themes. In this article I've taken away the common "but I already have so many posts without the format set" excuse.

Now, you have have very few excuses not to get on board with post formats.

They make your life easier as a site manager, and they allow you to make themes for others that serve the end-user better than any system relying on custom post types or custom taxonomies ever can.

If you're worried about losing the ability to organize a site by custom post type or custom taxonomy, don't! The final part of this series will show you how to do that with post formats, because sometimes you can have it all.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.