I once created a script for a website of mine that looks at who (which author) saves the page. Based on this information, a script ensures that the page that this author creates is automatically saved under his main (parent) page.
SCRIPT
function parentsetter_save_post()
{
global $post;
$custom_field = get_post_meta($post->ID,'provider_name',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));
}
}
}
add_action( 'wp_insert_post', 'parentsetter_save_post' );
However, after the publication of this page something strange happens with the permalink.
Example.
I am logged in as "Wesley" and if I save a page with the title "USB-Stick" the permalink should be as follows:
websiteurl/author-names/wesley/usb-stick/
But when I save the page the permalink is like this:
websiteurl/usb-stick/
When I try to go to this page I get a 404 because it doesnt exist.
The strange thing is that after 5/10 minutes the permalink is good and everything is fine.
But I get complains from everybody after publishing the page / post the link is not right and they get an 404.
Anybody has any idea how this happening and is there a way to solve this?