0

I used this code to make a custom post status which is public so that the post is still available for people to view.

// Define new awaiting status
function my_custom_post_status(){
   register_post_status( 'awaiting', array(
      'label'                     => _x( 'Changes Awaiting Approval', 'custom-post-type' ),
      'public'                    => true,
      'exclude_from_search'       => false,
      'show_in_admin_all_list'    => true,
      'show_in_admin_status_list' => true,
      'label_count'               => _n_noop( 'Changes Awaiting Approval <span class="count">(%s)</span>', 'Changes Awaiting Approval <span class="count">(%s)</span>' ),
   ) );
}
add_action( 'init', 'my_custom_post_status' );

Then I added this code so that when a certain role saves a post, the post status is changed from published to awaiting

function published_to_pending($post_id) {
    global $post, $current_user, $wp_meta_boxes;

    if (!is_object($post))
        $post = get_post($post_id);
    //if ( !current_user_can( 'edit_post', $post_id ) )
    //    return $post_id;
    // check for specific user that needs this publish to pending. otherwise exit
    if (get_current_user_role() != "speaker") {
        return;
    }
    if ($post->post_status == "publish") {
        global $wpdb;
        $result = $wpdb->update(
                $wpdb->posts,
                array('post_status' => "awaiting"),
                array('ID' => $post_id)
        );
      
    }
}

add_action('post_updated', 'published_to_pending', 13, 2);

function get_current_user_role() {
    global $wp_roles;
    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    $role = array_shift($roles);

    return $role;
}

The idea is then that an admin would then see the post with the revisions added to it and then put it back to the published state to then approve the changes and make them visible to all visitors of the website.

However, as the last step, I now want to allow the user who updated the post to see how their post will now look with their proposed revisions before it is approved by the admin. So essentially I was imagining it would work that if the post has an awaiting status:

  • For any public visitors, they would see the last version of the post that had the published status.
  • For admins and the author of the post, they would see the latest version of the post with the revised changes (so essentially the standard behaviour)

How would I go about achieving this? Would this be something I would need to add to the single.php page of the theme or would I need to make a custom query elsewhere?

0

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.