Support

Account

Home Forums Backend Issues (wp-admin) Update field in category by functions.php on category save?

Helping

Update field in category by functions.php on category save?

  • I’have some problem with my WordPress functions.php script.

    When I go to edit Category of Post, I have URL:

    /wp-admin/term.php?taxonomy=category&tag_ID=3&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

    I need a function to update the field in category with ID from URL.

    function overwrite_ratings_category(){
    	update_field('field_630dbc7cf3fdd', '432', 'category_'.$_GET['tag_ID']);
    }
    
    add_action('edit_term','overwrite_ratings_category');

    When I use number (e.g. 3) instead $_GET[‘tag_ID’] function works fine. But it’s not working with $_GET[‘tag_ID’].

    Is there any other way to get the ID of the currently edited category?

  • You should use the acf/save_post hook with a priority > 10

    
    function overwrite_ratings_category($post_id){
      if (is_string($post_id) && substr($post_id, 0, 5) == 'term_') {
        update_field('field_630dbc7cf3fdd', '432', $post_id);
      }
    }
    
    add_action('acf/save_post','overwrite_ratings_category');
    

    This might change depending on what type of field you are trying to update. What type of field are you trying to update?

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.