Assign a new template to ALL posts retroactivel9y

Posted in: Newspaper
Post count: 17

Hi, wondering if there is a way to assign a cloud template to ALL posts on our site (1,100 or so) at once. Some of the earlier posts seem to still be using the template they were originally assigned instead of the cloud template we are using now. Is there any way to do this programmatically, instead of going back to update each one individually? Thanks for any guidance you can offer on this.

Post count: 27744

Hello,

this is the code, I hope it helps you.
function edit_template(){
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );

while($the_query->have_posts()) {
$the_query->the_post();
global $post;

$td_post_theme_settings = get_post_meta($post->ID, 'td_post_theme_settings', true);

if( !empty($td_post_theme_settings['td_post_template'] ) ) {
$td_post_theme_settings['td_post_template'] = 'tdb_template_71018';
update_post_meta($post->ID, 'td_post_theme_settings', $td_post_theme_settings);
}
}
wp_reset_query();
}
edit_template();die;

This code must be set in functions.php and after you have set it, you can edit a post to see if the individual template has been reset, if so, then you can remove the code.

Thank you!

Post count: 17

Thank you, Anamaria. This is exactly what I was looking for. Really appreciate the assistance.

Post count: 90

Thanks, here is what my friend ChatGPT has allowed me to go through the code and update.

Though when you run it, it gives a white screen, you need to refresh the browser or open a new one.

/**
* Update post meta data for all posts of post type "post"
* with the "td_post_theme_settings" meta key.
* Specifically, if the post has a non-empty "td_post_template" setting,
* it is updated to "tdb_template_71018".
*/
function edit_template() {

// Define arguments for WP_Query
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);

// Create a new WP_Query object with the defined arguments
$the_query = new WP_Query( $args );

// Loop through each post in the query results
while( $the_query->have_posts() ) {

// Set up the post object and make it available to functions
$the_query->the_post();
global $post;

// Get the "td_post_theme_settings" meta data for the current post
$td_post_theme_settings = get_post_meta( $post->ID, 'td_post_theme_settings', true );

// If the post has a non-empty "td_post_template" setting,
// update it to "tdb_template_71018" and save the new value
if( !empty( $td_post_theme_settings['td_post_template'] ) ) {
$td_post_theme_settings['td_post_template'] = 'tdb_template_71018';
update_post_meta( $post->ID, 'td_post_theme_settings', $td_post_theme_settings );
}
}

// Reset the query and clean up after the loop
wp_reset_query();
}

// Call the function to execute the code
edit_template();

// Use "die" to ensure that no further code is executed after the function call
die;

Viewing 4 posts - 1 through 4 (of 4 total)
The forum ‘Newspaper’ is closed to new topics and replies.