How to update page templates in bulk with WP CLI in WordPress

 1.11.2023

Save the Earth for future generations.

<span data-tpgb-dynamic='{“dynamicField”: “period_length”, “titlePre”:””, “titlePost”:””, “dateFormat”:””, “oldVal”: “No human technology can replace `nature`s technology`, perfected over hundreds of millions of years to sustain life on Earth. For those in power, the questions are straightforward. Are they prepared to jeopardise their careers – or their profits – for our children’s children? Are they ready to put short-term politicking aside and help deliver a sustainable plan for the future? Are they willing to take difficult decisions on behalf of voters they’ll never meet?”, “incContext”:true, “proTerm”: “product_cat”, “termSep”:”,”, “priceFor”: “both”, “stText”:true, “ratFormat”: “average_rating”, “advsetting”:true, “fallBacktxt”:””}’></span>

Save the Earth for future Generations.

<span data-tpgb-dynamic='{“dynamicField”:”meta-markkinointilinkit”,”titlePre”:””,”titlePost”:””,”dateFormat”:””,”oldVal”:”No human technology can replace `nature`s technology`, perfected over hundreds of millions of years to sustain life on Earth. For those in power, the questions are straightforward. Are they prepared to jeopardise their careers – or their profits – for our children’s children? Are they ready to put short-term politicking aside and help deliver a sustainable plan for the future? Are they willing to take difficult decisions on behalf of voters they’ll never meet?”,”incContext”:true,”proTerm”:”product_cat”,”termSep”:”,”,”priceFor”:”both”,”stText”:true,”ratFormat”:”average_rating”,”advsetting”:false,”fallBacktxt”:””}’></span>

Save the Earth for future generations.

<span data-tpgb-dynamic='{“dynamicField”: “jakso_intro_short”, “titlePre”:””, “titlePost”:””, “dateFormat”:””, “oldVal”: “No human technology can replace `nature`s technology`, perfected over hundreds of millions of years to sustain life on Earth. For those in power, the questions are straightforward. Are they prepared to jeopardise their careers – or their profits – for our children’s children? Are they ready to put short-term politicking aside and help deliver a sustainable plan for the future? Are they willing to take difficult decisions on behalf of voters they’ll never meet?”, “incContext”:true, “proTerm”: “product_cat”, “termSep”:”,”, “priceFor”: “both”, “stText”:true, “ratFormat”: “average_rating”, “advsetting”:false, “fallBacktxt”:””}’></span>

Say you have 50 posts in your WordPress and you want to update to a new template? You could update them in the admin panel, but that would mean a lot of clicking around.

Luckily there’s an easy one liner that you can use to update page templates in bulk. You’ll need to have WP CLI installed for this to work. This one liner comes from this snippet by Mike Little and has been modified to work for template updates

How to update page templates multiple posts at a time with WP CLI

This is the one liner you can use to update ALL posts to use a new template.

for id in $(wp post list --post_type=post --fields=ID --format=ids); do wp post meta update $id _wp_page_template 'your-template-name-goes-here'; done

REMEMBER to change the template name (‘your-template-name-goes-here’) to match your own. Other than that it’s ready to go. I’d also do a trial run with the code below before you run through that because it updates all your posts at one go.

How to update page templates from a CSV list in WordPress

A slight tweak to the bulk meta update allows you to use a CSV list to target specific pages.

for id in $(wp post list --post_type=post --post__in=7482,7434,5470 --fields=ID --format=ids); do wp post meta update $id _wp_page_template 'your-template-name-goes-here'; done

After the post_in= section is where you can copy and paste the comma separated list you have from somewhere else. Remember to change the numbers to post IDs in your own system.

That’s it! Have fun.


Latest updates in your inbox

Subscribe and get the latest marketing goodies from Aalho Media

Contribute to the conversation


Leave a Reply

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