Skip to main content Skip to navigation Skip to footer

Update post format and type in bulk using WP-CLI

When setting up this site, one thing I wanted to do was import my tweets, 23,509 of them to be exact. I had already exported them from Twitter/X a while back and used Alex Standiford’s PR of Shawn Hooper’s excellent Tweet Importer plugin.
However, after the import completed, I wanted to move them from the custom post type they came into to posts instead, utilising WordPress’s post formats to style them as ‘status’ updates as opposed to ‘standard’ posts. Here’s the WP-CLI command I came up with to change their post format, title, post type, and category in bulk:

for post_id in $(wp post list --post_type=birdsite_tweet --format=ids)
do
	wp post term set $post_id post_format post-format-status
	wp post update $post_id --post_title="Tweet #"$post_id --post_type=post
        wp post term set $post_id category twitter
done

This will loop over all posts in the ‘birdsite_tweet’ custom post type and set the term to be ‘post-format-status’ on the ‘post_format’ taxonomy. It’ll then set the post title to “Tweet #XXXXX” and the post type to ‘post’.

I also had to use the following code to enable post formats everywhere it needed to be in order for this to work:

function jm87_post_type_supports() {
        add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
	add_post_type_support( 'birdsite_tweet', 'post-formats' );
	register_taxonomy_for_object_type( 'post_format', 'birdsite_tweet' );
}
add_action( 'init', 'jm87_post_type_supports', 9999 );

I can now style these tweets differently to standard format posts, and display additional info, such as the likes and retweets post meta that came across with them, without modifying the global query across the whole site to include the additional ‘birdsite_tweet’ post type.


Leave a Reply

Your email address will not be published. Required fields are marked *