Get Category in save_post Hook

in a class I have a function for save_post action. In this function I send an email. All this works except for querying the categories for that post:

class _new_notifications
{
    public function __construct()
    {
        add_action( 'save_post', array( $this, 'send_new_post_notifications' ) );

    }

    public function send_new_post_notifications( $post_id ){
        $send_notify = get_field('send_notifications', 'option');
        if ( !$send_notify || wp_is_post_revision( $post_id ) ) {
            return;
        }

        $categorie = get_the_category( $post_id );
        $cat = $categorie[0]-name;

//doing other stuff...

I already tried everything possible (several hours :) but I just do not get the category for that post ID.

Maybe someone can. help? Many Thanks!!

Topic save-post categories hooks posts Wordpress

Category Web


As @SallyCJ mentioned, you can not use get_the_category function there, instead you have to use wp_get_post_terms as shown below:

class _new_notifications 
{
    public function __construct()
    {
        //Setting up action so that it passes $post object as well, we can also set it to send a third variable defining update/publish status.
        add_action( 'save_post', array( $this, 'send_new_post_notifications',10,2 ) );
    }
    public function send_new_post_notifications( $post_id, $data ){
        $send_notify = get_field('send_notifications', 'option');
        if ( !$send_notify || wp_is_post_revision( $post_id ) ) {
            return;
        }
        $taxonomy = "<YOUR_TAXONOMY_SLUG_HERE>";
        //Updating arguments array to use all the post_category values added while saving the post. The first index in my belief is always containing zero as a value so iterate as you wish.
        $args = array('taxonomy' => 'category', 'object_ids' => $data['post_category']);
        //Using get terms because we now know the term ids.
        $categorie = get_terms($args );            
        $cat = $categorie[0]->name;
        //Ideally you should loop through $categoie array and push those value in $cat array but you can proceed from here on with however you wish.

Unfortunately, no success after a test :(

I put the follwing:

if ( get_post_status( $post_id ) === 'publish' ) {
                $post_title = apply_filters('the_title', get_post_field('post_title', $post_id));
                $post_content = apply_filters('the_content', get_post_field('post_content', $post_id));


                $taxonomy = "category";
                $args = array('orderby' => 'name', 'order' => 'ASC');
                $cat = wp_get_post_terms( $post_id, $taxonomy, $args );
                $categorie = $cat[0]->name;

I get the count of cat in the wp_mail in the body output - the count is at 0. Interestingly - when I change the taxonomy to Category (big C) - I get an array with an error. (Count 1 in the mail body)

I have written a test function that I do not run in save_post hook but by my own call - in this function, wp_get_post_terms works with the same post ID without errors.

An important note yet:

In the save_post function is a check - I compare get_post_time (Unix timetamp) with get_modified_time (Unix timestamp)

If this comparison shows a difference - is it a later update of the post - is the difference 0 is it a new post.

When I create a new post category does not work. When I update the post the function works for the category

Any other approaches that might help? (I also like to post the whole class here, if necessary)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.