• Resolved rmkahler

    (@rmkahler)


    I am creating a plugin that makes a custom post type. I need a function to run when one of these CPT posts is moved to the trash. However, when I trash one of these, the wp_trash_post hook does not work. If I trash a regular post, the function runs just fine, but not when I trash one of my CPTs.

    Further, untrash_post works with my CPT just fine.

    Any thoughts?

    • This topic was modified 8 years, 10 months ago by rmkahler.
Viewing 4 replies - 1 through 4 (of 4 total)
  • wp_trash_post seems to only work on posts and not on pages or cpt

    Thread Starter rmkahler

    (@rmkahler)

    Yes… I figured that out. I guess my question wasn’t clear. Is there another hook that will work for CPTs, or do I have to create a custom one?

    Moderator bcworkz

    (@bcworkz)

    “wp_trash_posts” fires on the trashing of any post, page, or CPT on my site. It should on yours too. If it is not, some other code has hooked into the process in a way that prevents your callback from being called. To narrow down what code is doing this, do the deactivate all plugins and use a twenty* theme troubleshooting process.

    Thread Starter rmkahler

    (@rmkahler)

    Thanks @bcworkz! It wasn’t another plugin conflict… but your response got me thinking about a conflict in my own plugin, which I hadn’t thought of before.

    The issue was that I have two functions – one that creates data in an API, and one that deletes that data from that API. But I was having the create function run with the save_post hook, which I now know runs any time a change is made to that post, including when the post is trashed. So when I trashed the post, both the delete and create functions ran, which resulted in no change to the data. I replaced save_post with publish_post and all is working properly now.

    Thanks for the help!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘wp_trash_post not working for CPT’ is closed to new replies.