Is it possible to automatically set/select a parent page on page creation when a custom field and page title are equal?
Example:
I have the following page hierarchy:
When a new page is created, and the custom field value is “user1” and there is a page with the title “user1”. Then the Provider Page, in this case “user1”, should automatically be set as the parent for the page that is created (Provider Package page).
I hope it is a bit clear because it is quite difficult to explain I notice myself.
Is such a thing possible?
You can achieve this by using post editing hook filters/actions. Like this
add_action( 'edit_post', 'parentsetter_save_post' );
function parentsetter_save_post()
{
global $post;
$custom_field=get_post_meta($post->ID,'customfieldname',true);
if ($custom_field!=''){
$parent_page=get_page_by_title($custom_field);
if (!empty($parent_page) and $post->post_parent!=$parent_page->ID){
global $wpdb;
$wpdb->query($wpdb->prepare("update $wpdb->posts set post_parent=%d
where ID=%d",$parent_page->ID,$post->ID));
}
}
}
Just replace "customfieldname" with your custom field name and it will work.