• Resolved charafweb

    (@charafweb)


    Hi friends,
    I’m a noob, and I have a question: Is it possible to get the ID of trashed posts from functions.php when a visitor try to access it?

    I’m using this in my functions.php

    function redirect_trashed_post(){
        if( is_404() ){
        	global $wp_query, $wpdb, $post;
            $id = get_the_ID(); // <<--- trying to get this: the id of the current post
            // some other codes here...
        	}
    }
    add_action('template_redirect', 'redirect_trashed_post');

    I tried all the bellow methods but in vain:

    (1) global $post; $post_id = $post->ID();
    (2) global $wp_query; $post_id = $wp_query->get_queried_object_id();
    (3) global $wp_query; $post_id = $wp_query->post->ID;
    (4) $post_id = get_the_ID();
    (5) $post_id = get_query_var(‘page_id’)

    Please help

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    That’s not how requests that go 404 work. WP queries for the requested post by specifying that we want a post matching the request and where the status is “publish”. If nothing is returned, WP does the 404 thing. At no time in this process does WP know if a post is trashed, that is not what we’re typically looking for.

    To get the trashed post ID, you’d need to redo the query, asking for the requested post where the status is “trash”. If anything comes back, you have a trashed post.

    Thread Starter charafweb

    (@charafweb)

    Hi @bcworkz ,
    The following code (found on github) that make me think that we can access trashed posts IDs when is_404 :

    add_action('template_redirect', 'trash_redirect');
    function trash_redirect(){
        if (is_404()){
            global $wp_query, $wpdb;
            $page_id = $wpdb->get_var( $wp_query->request );
            $post_status = get_post_status( $page_id );
            if($post_status == 'trash'){
                wp_redirect(home_url(), 301);
                die();
            }
        }
    }

    But even this code is not working.
    And I’m still trying and trying ??

    Thread Starter charafweb

    (@charafweb)

    Hi @bcworkz
    I think this is what you mean:

    add_action('template_redirect', 'trash_redirect');
    function trash_redirect(){
    	$get_slug = trim($_SERVER['REQUEST_URI'], '/');
        if (is_404()){
            global $wpdb;
    
            $page_id = $wpdb->get_var(
    		"
    		SELECT ID
    		FROM $wpdb->posts
    		WHERE post_name = '$get_slug'
    		"
    		);
    
            $post_status = get_post_status( $page_id );
            if( $post_status == 'trash' ){
                wp_redirect(home_url(), 301);
                die();
            }
        }
    }

    But it’s not working and I don’t know why. Any Idea?
    Thanks

    • This reply was modified 6 years ago by charafweb.
    • This reply was modified 6 years ago by charafweb.
    • This reply was modified 6 years ago by charafweb.
    Moderator bcworkz

    (@bcworkz)

    WP renames the trashed slugs by appending __trashed to the original slug. Query for that name ?? Note there are 2 underscores there.

    Thread Starter charafweb

    (@charafweb)

    The query:

    $wpdb->get_var(
    "
    SELECT ID
    FROM $wpdb->posts
    WHERE post_name = '$get_slug'
    "
    );

    won’t return anything if the post is trashed because slug of trashed post has a suffix “__trashed” that I did not included in the provided slug.

    So, we have to change the slug as bellow:

    $get_slug = trim($_SERVER['REQUEST_URI'], '/') . '__trashed';

    Thread Starter charafweb

    (@charafweb)

    ?? I found it at the same time as you ??
    Thanks @bcworkz

    Moderator bcworkz

    (@bcworkz)

    Great minds think alike ?? You’r welcome.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to get the ID of a trashed post from functions.php’ is closed to new replies.