0

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!!

  • You should know that get_the_category() does not work for custom taxonomies, and that the save_post hook is fired for any post types. – Sally CJ Jul 1 '19 at 23:49
  • Thx Sally CJ, in my Case i dont use custom Taxonomys (only a Custom Postype) and this Posttype use the normal Wordpress Categorys.. – Ivo Poe Jul 2 '19 at 11:02
0

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)

| improve this answer | |
0

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.
| improve this answer | |
  • To avoid misunderstandings: I use a custom posttype but NOT a custom taxonomy. I use the Wordpress Standard category for the posttype. In another function elsewhere get_the_Category works without problems - just not in the save_post action (I've removed the posttype check here in the code - this is of course in it so that the save_post action only responds to this posttype) – Ivo Poe Jul 2 '19 at 11:08
  • @IvoPoe Please check my updated answer. I personally verified that post_category is available for a custom post type so this should resolve your problem. – Faham Shaikh Jul 2 '19 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.