I am trying to create a function that REDIRECTs Draft and Trashed Posts to a custom field I have in all of my posts.

I am calling this function at the start of header.php, however the webpage just shows the standard 404 Page. With this I cannot call any custom fields I have. I can make it work if I have the POSTID but cannot get that on a 404 page.

Advice appreciated, I have spent several hours on this.

The following function was taken from another user I found.

//redirect any draft/trashed posts to custom postmeta field: redirect_url_on_draft
add_action('wp', 'custom_redirect_on_draft');
function custom_redirect_on_draft(){
    if ( !current_user_can( 'edit_pages' ) ) {
        if (is_404()){
            global $wp_query, $wpdb;
            $page_id = $wpdb->get_var( $wp_query->request );
            $post_status = get_post_status( $page_id );
                $post_redirect = get_post_meta( $page_id, 'redirect_url_on_draft', true );
            if(($post_status == 'trash' || $post_status == 'draft') && $post_redirect){
                wp_redirect($post_redirect, 302);


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.

Browse other questions tagged or ask your own question.