TransWikia.com

Get categories from save_post hook

WordPress Development Asked by malago on October 30, 2021

I am trying to develop a hook when save_post to get the categories but for new posts I don’t get any categories and for updated posts I get the categories "before" updating the post. This should be super simple but I can’t figure it out. I have been hours trying to find a solution.

This is the minimal code that I am trying to run:

add_action('save_post', 'test' , 10, 3 );
   
   function test($post_id, $post, $update){
       $categories=get_the_category($post_id);
       var_dump($categories);
   }

I am also trying to get things from $_REQUEST but its only content is this:

Array
(
    [_locale] => user
)

What am I doing wrong here?

One Answer

After doing much more search... it looks like the save_post hook does not run after the post categories are updated so it will only show the categories that the post had before the update (or no categories for new posts).

I found that I need to use this hook and then everything inside my function is updated:

add_action('rest_after_insert_post', 'test' , 10, 2 );

I had no idea that was the hook I needed to use. I hope this helps someone!

Answered by malago on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP