WordPress.org

Ready to get started?Download WordPress

Forums

Restoring a trashed custom post via "undo" vs. going to the trash and restoring (2 posts)

  1. Sovereign
    Member
    Posted 1 year ago #

    I've got the following action in place:

    add_action(
    	'untrash_post',
    	array($this, 'cpt_contact_restore')
    );

    Which executes the following function:

    function cpt_contact_restore() {
    	if(get_post_type() == 'cfe-cpt-contact') {
    		$this->cpt_contact_set_display_status(1);
    	}
    }

    This function fires when restoring a custom post via the "undo" link that's visible right after trashing a post and when going to the trash and restoring it via the action link available under the post title.

    After I added the get_post_type() check, so this function only fires when needed, it stopped working when using the "undo" link, but it continues to work when restoring a post by going to the trash.

    It would seem that both methods of restoring a post should be consistent, if not identical.

    I think the problem is that get_post_type() is not set or something.

    Does anybody have any ideas of what I can do to solve this?

  2. Sovereign
    Member
    Posted 1 year ago #

    After a bit more search I noticed that when using the "restore" action link it looks as follows:

    post.php?post=209&action=untrash&_wpnonce=ddb4c9c68b

    But when using the "undo" link it looks like this:

    edit.php?post_type=cfe-cpt-contact&doaction=undo&action=untrash&ids=209&_wpnonce=ea0d2a97d2

    So while I still believe that get_post_type() should work in this case as well, I simply added another check for the query string value post_type:

    if(get_post_type() == 'cfe-cpt-contact' || $_GET['post_type'] == 'cfe-cpt-contact')

Topic Closed

This topic has been closed to new replies.

About this Topic