0

I'm using the transition_post_status to prevent a custom post type from being published based on a post meta field. It doesn't seem to work entirely, and that's where I need some help.

Here is a simplified version of what I have:

add_action( 'transition_post_status', [ $this, 'intercept_adherence_publishing' ], 9, 3 );

function intercept_adherence_publishing( $new_status, $old_status, $post ) {
        $post_id = $post->ID;
        $adherence_status = $_POST['_adherence_status'];

        if ( ( $new_status === 'publish' ) && ( $post->post_type == 'protocol-adherence' ) && ( $adherence_status !== 'accepted' ) ) {
            error_out('Published post ' . $post_id . ' intercepted. Post remains unpublished due to adherence not being accepted');
            wp_die( '<b>Adherence Error: </b>Cannot publish adherence that is not accepted. Please save status in "Pending Review" instead of the publish button if the adherence is not accepted yet.', 'Adherence Publishing Error', [ 'back_link' => true ]);
        }
    }

What is supposed to happen when post meta is not "accepted":

  • wp_die() displays the admin error with a link to return to the post
  • post remains in pending review state with the "Save as pending" option
  • publish date still shows as "Publish immediately" above the publish button

What is actually happening with this code:

  • wp_die() displays the admin error with a link to return to the post (which is happening and is good)
  • post status goes from "pending review" to "published" and "save as pending" button disappears. (not good)
  • publish button now has a date above it saved like "Publish on Jan 10, 2019 @ 14:46" instead of retaining it's "Publish immediately" status (not good)

Is there another hook I should be using or some additional logic I'd have to write in this function so that the post truly remains unpublished? As in we keep the "pending review" status/option and prevent a date from being saved?

Thanks in advance!

0

I think that hook fires after the post status has been updated. Try this

add_action( 'pre_post_update', 'intercept_adherence_publishing ', 10, 2);

function intercept_adherence_publishing ($post_ID, $data ) {
    if (get_post_type($post_ID) !== 'protocol-adherence`) {
        return;
    }
    $post = get_post($post_ID);
    $adherence_status = $_POST['_adherence_status'];

    if ( ( $data['post_status'] === 'publish' ) && ( $post->post_type == 'protocol-adherence' ) && ( $adherence_status !== 'accepted' ) ) {
        error_out('Published post ' . $post_ID . ' intercepted. Post remains unpublished due to adherence not being accepted');
        wp_die( '<b>Adherence Error: </b>Cannot publish adherence that is not accepted. Please save status in "Pending Review" instead of the publish button if the adherence is not accepted yet.', 'Adherence Publishing Error', [ 'back_link' => true ]);
    }
}
  • Does this hook also fire on trying to publish from quick edit or will I have to find a separate hook? Thanks. – RachieVee Jan 10 at 21:11
  • 1
    this hook fires on post update so it's checking this thing FAR more often than necessary. I'm actually going to edit the answer to short circuit out of the filter earlier if it's not your CPT. – mrben522 Jan 10 at 21:17
  • 1
    To get this code working for quick edit as well, instead of $data use, $_REQUEST to check the post meta. And instead of using $data to check the post status, use get_post_status( $post_ID ). – RachieVee Jan 11 at 17:21

Your Answer

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

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