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:
$videos = array( // put a comma separated list of posts IDs here ); $format = 'video'; $videos = $posts; foreach ( $posts as $post ) { set_post_format( $post, $format ); }
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()
.
// Set the post type, taxonomy, and taxonomy term for which to query // Note: you can set taxonomy and term by ID or slug $posts = array( 'post_type' => 'post', 'taxonomy' => 'post-type', 'term' => 'videos' ); // Set the post format to assign $format = 'video'; $args = array( 'post_type' => $posts[ 'post_type' ], 'tax_query' => array( array( 'taxonomy' => $posts[ 'taxonomy' ], 'field' => 'slug', 'terms' => $posts['term'], ), ), ); $query = new WP_Query( $args ); // Loop on the queried posts if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); set_post_format( $query->post->ID, $format ); } // endwhile } // endif have posts
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.
Comments