Archived topic
Programmatically set all pages up to use sections?
5 replies · Started by Katie Jones on March 22, 2017
Hi all,
Has anyone developed code to programmatically set up all pages to use sections? I have a site with 40+ pages where I'd like to, for each page, set the page to use sections, and create a first section with the content from the post content. Does this exist somewhere already?
Thanks!
Katie
Hi Katie,
I'm afraid this doesn't currently exist. The Sections settings would also need to be set up etc.. automatically.
Thanks, Tom. Any reason you can think of that this can't be done programmatically, or would be especially difficult?
I dug into how this works in the db maybe a year ago but I've forgotten. Seems like I might have tried this back then and ran into something unexpected though I don't remember what or why that would be.
You would basically have to loop through each of your posts:
$posts = get_posts();
foreach ( $posts as $post ) {
}
get_posts: https://codex.wordpress.org/Template_Tags/get_posts
Then in there, update the post meta to turn sections on:
update_post_meta( $post->ID, '_generate_use_sections', array( 'use_sections' => 'true' ) );
Totally untested, but theoretically that should turn Sections on within each page you looped through.
The hard part is taking your content from the default editor, and adding it into the first section. I don't know how to do that off the top of my head, would take quite a bit of trial and error to figure out.
Hmm, maybe that second bit was the part I tripped on last time. Okay, I'll likely be trying this, if I do get it working I'll post my code. Thanks, Tom!
Sounds good! :)