update field inside custom “set_object_terms” hook function
-
Bonjour!
I am trying to rewrite this custom
set_object_termshook function.
What I am trying to achieve in addition to Automatically check child categories/terms when parent category is checked as this function already is doing, is to also update the GeoDirectory fieldpost_categorywith the same data as the updated values for:$categories = get_the_terms( $post->ID, 'gd_placecategory' );Note that Geo Directory has it own set of get/delete/save functions, I have tested them from a template . What I have tried in order to solve this issue is to create an
$mergeand$diffvariable for add and remove, and then trying to add this to thepost_categoryfield by using thegeodir_save_post_meta()function. I have also tried to perform the update by using the$wpdb->update()as well.add_action( 'set_object_terms', 'so_60079535_toggle_child_terms', 10, 6 ); function so_60079535_toggle_child_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ){ global $wpdb; // Abort if this is an autosave/backup if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; // Abort if no ids are set from before or now if( empty($tt_ids) && empty($old_tt_ids) ) return; // Only do things if this post is published (front facing) $post_status = get_post_status( $object_id ); if( $post_status != 'publish' ) return; // What terms where ADDED, and which were REMOVED? $added_terms = array_diff_once( $tt_ids, $old_tt_ids ); $removed_terms = array_diff_once( $old_tt_ids, $tt_ids ); // Any terms ADDED this time? if( !empty($added_terms) ){ $add = array(); foreach( $added_terms as $added_term ){ // Do any of these added terms have children? if( $added_child_terms = get_term_children( $added_term, $taxonomy ) ){ // Append those children wp_set_object_terms( $object_id, $added_child_terms, $taxonomy, true ); $add[] = $added_child_terms; } } if( !empty($add) ){ $add = $add[0]; $merge = array_merge($old_tt_ids,$add); $merge = implode(",",$merge); if( !empty($merge) ){ geodir_save_post_meta($object_id, 'post_category', $merge); //$wpdb->update( 'wp_geodir_gd_subscription_detail', array( 'post_category' => $merge), array('post_id' => $object_id )); } } } // Any terms REMOVED? if( !empty($removed_terms) ){ $remove = array(); foreach( $removed_terms as $removed_terms ){ // Do any of the removed terms have children? if( $removed_child_terms = get_term_children( $removed_terms, $taxonomy ) ){ // Remove them all wp_remove_object_terms( $object_id, $removed_child_terms, $taxonomy, true ); $remove[] = $removed_child_terms; } } if( !empty($remove) ){ $remove = $remove[0]; $diff = array_diff($old_tt_ids,$remove); $diff = implode(",",$diff); if( !empty($diff) ){ geodir_save_post_meta($object_id, 'post_category', $diff ); //$wpdb->update( 'wp_geodir_gd_subscription_detail', array( 'post_category' => $diff), array('post_id' => $object_id )); } } } }When either the
geodir_save_post_meta()function or the$wpdb->update()is added to the function, this custom function does run, but only the terms are changed. None of theset_object_termsgenerated terms is added to thepost_categoryfield.It seems to me that the
set_object_termshook is not the place to run a update from. Still I need this hook to perform the Automatically check child categories/terms when parent category is checked.How can I both Automatically check child categories/terms when parent category is checked and update the GeoDirectory field
post_categoryat the same time?
The topic ‘update field inside custom “set_object_terms” hook function’ is closed to new replies.