Copy Cornerstone template between pages programmatically?

I have 145+ pages I need to update with a new CS template and would rather not do it by hand!
I’ve tried and got most of the way there but there’s something missing. here’s what I have so far:

  • copy page content
  • copy ‘_cornerstone_data’ and ‘_cornerstone_settings’ post meta fields
  • delete ‘_cornerstone_override’ post meta if it exists

it so nearly works! one problem is that while the CS template is applied (mostly), it can’t be edited in CS.
I’m using ACF Advanced Custom Fields functions because they enable post meta arrays to be written directly.

$post_ID = 6039; //donor page
$post_ID2 = 7821; //recipient page

//copy page content between pages
$content_post = get_post($post_ID);
$content = $content_post->post_content;
$my_post = array(
‘ID’ => $post_ID2,
‘post_content’ => $content
);
wp_update_post( $my_post );

//use ACF functions to copy the 2 post meta fields containing CS template config
$cornerstone_data = get_field( ‘_cornerstone_data’, $post_ID);
update_field( ‘_cornerstone_data’, $cornerstone_data, $post_ID2);

$cornerstone_settings = get_field( ‘_cornerstone_settings’, $post_ID);
update_field( ‘_cornerstone_settings’, $cornerstone_settings, $post_ID2);

//use ACF functions to delete ‘cornerstone_override’ post meta if it exists (to avoid ‘content has be modified outside CS’ error)
if ( get_field(’_cornerstone_override’, $post_ID2) ) { //field exists
delete_field(’_cornerstone_override’, $post_ID2);
}

echo ‘Post CS template copied!’;

Hi @theabsurdman,

Thanks for reaching out.
I would suggest you save the page as template and load that specific template in the target page. You can get more information on how to save and use the template in this article: https://theme.co/docs/template-manager#how-to-save-the-templates.


Alternatively, you can use a few free plugins from WordPress repository, which will help you to copy/clone the specific page or post along with the settings.

  1. https://wordpress.org/plugins/duplicate-wp-page-post/
  2. https://wordpress.org/plugins/duplicate-page/
  3. https://wordpress.org/plugins/duplicate-post/

Please remember that we don’t offer any support to custom codes or 3rd party plugins related issues.

Hope this helps.
Thanks

thanks for your suggestion Tristup but sadly it does not address my problem.

I already have 145+ pages created - complete with ACF fields - and just need to update their CS templates (nothing else).

My only option at the moment for this is to open each one in CS and apply the new template manually, which will be laborious, to say the least!

What I want to do instead is to PROGRAMMATICALLY apply the new CS template to all pages in a script.

I’m pretty sure it can be done as I’m 90% of the way with the code I posted - based on a forum post by one of your devs, Alex:

Could you ask one of your Cornerstone developers to look at my code and see where I am going wrong please?

I’m sure other users would appreciate this functionality.

Note, I’m using an older verion of Cornerstone - Version 1.3.3.

Cheers,
John

Hi John,

What you are expecting is required customization and beyond the scope of our theme support, I would suggest you hire a developer who can assist you to do the customization or you can avail of our newly launched service called One, where the customization questions are answered.

Thanks

Fair enought, let’s leave it here then and see if any other forums members want to run with it.

If and when i do find a definitve solution, I’ll update the post.

Rgds,
John

Hi John,

Great, If you have any other concerns regarding our theme features, feel free to reach us.

Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.