I’m attempting to create a operate that REDIRECTs Draft and Trashed Posts to a customized subject I’ve in all of my posts.
I’m calling this operate in the beginning of header.php, nonetheless the webpage simply reveals the usual 404 Web page.
With this I can not name any customized fields I’ve. I could make it work if I’ve the POSTID however can not get that on a 404 web page.
Recommendation appreciated, I’ve spent a number of hours on this.
The next operate was taken from one other person I discovered.
//redirect any draft/trashed posts to customized postmeta subject: redirect_url_on_draft
add_action('wp', 'custom_redirect_on_draft');
operate custom_redirect_on_draft(){
if ( !current_user_can( 'edit_pages' ) ) {
if (is_404()){
world $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);
die();
}
}
}
}