Support

Account

Home Forums General Issues Update post category description by saving content in ACF field

Solved

Update post category description by saving content in ACF field

  • Hello,

    I’m trying to update default post category descriptions by saving content in a separate ACF field. I’ve successfully used similar logic on pages and single posts when updating “the_content” by saving content in a separate ACF field (this came in handy for keeping the backend editor nice and tidy), but I haven’t been able to tweak that same code to work with post category descriptions. Could someone tell me where I’m going wrong?

    In the example below, I’m trying to use the custom field I created (custom_page_explainer) to update the native category description field when I click save.

    add_filter(‘acf/update_value/name=custom_page_explainer’, ‘acf_save_description’, 10, 3);
    function acf_save_description($term_id, $taxonomy, $args) {
    $term_id = get_queried_object()->term_id;
    $taxonomy = ‘product_cat_’.$term_id;
    wp_update_term($term_id, $taxonomy, array(
    ‘description’ => $value
    ));
    return $value;
    }

    Thanks for your help!

  • https://www.advancedcustomfields.com/resources/acf-update_value/

    
    add_filter('acf/update_value/name=custom_page_explainer', 'acf_save_description', 10, 3);
    function acf_save_description($value, $post_id, $field) {
      if (substr($post_id, 0, 5) != 'term_') {
        return $value;
      }
      $term_id = intval(substr($post_id, 5));
      //.... 
    }
    
  • I didn’t mean to click “solved”… =/

    This is not working for me. Any chance you could elaborate? I am trying to update a category’s default description field, when I enter content into a custom ACF field.

  • I was trying to show you where you’re going wrong.

    You have the wrong arguments in your function…

    You have listed

    
    function acf_save_description($term_id, $taxonomy, $args)
    

    and it should be

    
    function acf_save_description($value, $post_id, $field)
    

    You are using get_queried_object() and this may not work in an acf/update_value hook.

    I was trying to show you how to get the term ID that is being updated by using the $post_id value that ACF is passing to your filter.

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

The topic ‘Update post category description by saving content in ACF field’ is closed to new replies.