Friday, August 12, 2022
HomeWordPress DevelopmentRedirect DRAFT Posts to a Customized URL (Customized Discipline) : At present...

Redirect DRAFT Posts to a Customized URL (Customized Discipline) : At present 404 Web page


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();
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments