0

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.com/author-names/wesley/usb-stick/

But when I save the page the permalink is like this:

websiteurl.com/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?

  • Welcome to WPSE. Please share the script(s) you have added to the site - otherwise we don't have much chance of determining what may be happening. – WebElaine Sep 25 at 13:59
  • @WebElaine, thank you for your response. I added the script that is running in functions.php. It is working correct but for some reason with some kind of delay and I dont know why exactly. – user2812779 Sep 26 at 7:24
  • You may need to log some of the hooks that fire when you save a post. For example, save_post, transition_post_status, wp_insert_post. If you just write to a single log file and have it say what hook fired at what timestamp, and append each time, that may help you figure out what steps are happening when, and why there seems to be a delay. – WebElaine Sep 30 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.