functions – PHP variable not regenerating when publishing multiple posts at the same time


Context: I am building an automated system which will grab record from a database and immediately publish them en masse as a CPT. When they are being published, I have a function in the functions.php file of a child theme that should automatically set two categories to the post, a “fixed” one called ‘People’, and another one based on a column (categ) which is part of the record sent with the post.

My issue is that this second category (which should change per the contents of the record) is being permanently set for all the posts as the first one being processed. Is there a way to modify the code below to better specify that it should return the category found in that specific record? Is it possibly an issue with using the ‘save_post’ hook for publishing multiple posts at the same time?

Content in functions.php

function post_auto_categ( $post_ID ) 
    global $wpdb;
    $user_ID = get_current_user_id();

    $categname = $wpdb->get_var("SELECT categ FROM wp_people_posting");
    $categnum = $wpdb->get_var("SELECT term_id FROM wp_terms WHERE name="" . $categname .""");

    $peoplenum = $wpdb->get_var("SELECT term_id FROM wp_terms WHERE name="People"");
    $post_categories=array($peoplenum, $categnum);

        wp_set_post_categories( $post_ID, $post_categories );
   return $post_ID;
add_action( 'save_post', 'post_auto_categ' );


enter image description here

Attribute with category content:

enter image description here

Any suggestions would be greatly appreciated. Thank you.

UgKnu 2 months 2021-05-17T17:33:22-05:00 0 Answers 0 views 0

Leave an answer