0

I'm having difficulty getting a function to fire when publishing a new page. I'd like it to fire when a new page is being published or edited but ONLY when that page has a parent.

Here's what I've got

function myFunction(){
    global $post; 
    if($post->post_parent > 0){
        // do stuff
    }   
}
add_action( 'publish_page', 'myFunction' );

So far this works when I'm updating a page, but not when I'm publishing one for the first time, it's not recognising that it has a parent at that point I suppose?

1 Answer 1

0

The publish_page action is listed as deprecated. You can use the 'transition_post_status' hook to check if a page was published.

function publish_page_interception( $new_status, $old_status, $post ) {
    if ( ($new_status != $old_status) && ($post->post_status == 'publish') && ($post->post_type == 'page') ) {
        if($post->post_parent > 0) {
            //do stuff
        }
    }
}
add_action( 'transition_post_status', 'publish_page_interception', 10, 3 );

Look here for further info: http://codex.wordpress.org/Post_Status_Transitions

Happy Coding,

Kuchenundkakao

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.