• Resolved matpakka

    (@matpakka)


    Bonjour!

    I am trying to rewrite this custom set_object_terms hook 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 field post_category with 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 $merge and $diff variable for add and remove, and then trying to add this to the post_category field by using the geodir_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 the set_object_terms generated terms is added to the post_category field.

    It seems to me that the set_object_terms hook 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_category at the same time?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Stiofan

    (@stiofansisland)

    Hi @matpakka,

    I would suggest you look at this hook and set the terms of the array as needed.
    https://github.com/AyeCode/geodirectory/blob/master/includes/class-geodir-post-data.php#L419

    if you need help just ask.

    Thanks,

    Stiofan

    Thread Starter matpakka

    (@matpakka)

    Thanks for your answer, not entirely sure how to customize this geodir_save_post_temp_data filter to update the post_category field?

    Plugin Author Stiofan

    (@stiofansisland)

    No probs, let me give a basic example. All post info is contained in this array, so…

    add_filter('geodir_save_post_temp_data','_my_post_cat_stuff',10,3);
    function _my_post_cat_stuff( $gd_post, $post, $update){
    
        // set some new cats by id
        $gd_post['post_category'] = "123,321,444,55,6";
    
        // specifically set the default cat (sometimes used in the URL) (optional)
        $gd_post['default_category'] = 6;
        
        return $gd_post;
    }

    if you have any questions or need more help just ask.

    Thanks,

    Stiofan

    Thread Starter matpakka

    (@matpakka)

    There is something I am not getting with this geodir_save_post_temp_data filter. With this code below, I am only updating the post_category with the previous values.

    add_filter('geodir_save_post_temp_data','_my_post_cat_stuff',10,3);
    function _my_post_cat_stuff( $gd_post, $post, $update){
    
        global $gd_post;
    
        $categories = get_the_terms( $gd_post->ID, 'gd_placecategory' );
    
        $term_id = array();
        if($categories) {
            foreach( $categories as $term ) {
                $term_id[] = $term->term_id;
            }
        }
    
        $output = implode(",",$term_id);
        custom_logs("CATEGORY OUTPUT");    
        custom_logs($output);
    
        // set some new cats by id
        $gd_post['post_category'] = $output;
    
        // specifically set the default cat (sometimes used in the URL) (optional)
        $gd_post['default_category'] = $output[0];
        
        return $gd_post;
    }

    Is there a way I could pass arguments from my custom set_object_terms hook to the geodir_save_post_temp_data filter?

    • This reply was modified 6 years ago by matpakka.
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘update field inside custom “set_object_terms” hook function’ is closed to new replies.