3

I am trying to save a revision of a custom post type. This is what I have to check if the post has a parent:

function save_revision( $post ) {

    if ( $post['submitted'] == true ) {
       $parent_id = wp_is_post_revision( $post['post_id'] );
       ...
    }
} add_action('init', 'save_revision');

The problem is that I always get false for $parent_id even though I know there is a parent post for that post_id.

Any help is appreciated, I have been trying to get this to work forever.

2 Answers 2

3

I know this is an old thread, but for future reference - the term "revision" in Wordpress is a little confusing. It's not the updated, or revised, post... but the older version. The most updated version is the "parent."

So, if you're calling the function wp_is_revision_post() on the current version of the post, it will always return false (and wp_get_post_parent_id() will return NULL) since the current post is not considered a "revision." You will need to call it on an actual revision, (which would have a different ID from the current version of the post).

1

It is simple: If $post is not a revision, wp_is_post_revision() returns false; just reading the name of the function give you the reason. wp_is_post_revision() returns the parent post ID only if the given post is a revision (a revision has always a parent post). It seems that you want to get the parent post ID of $post, not checking if $post is a revision.

To get the parent post ID of another post use wp_get_post_parent_id():

function save_revision( $post ) {

    if ( $post['submitted'] == true ) {
       $parent_id = wp_get_post_parent_id( $post['post_id'] );
       ...
    }
}
add_action('init', 'save_revision');

Your Answer

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

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