Support

Account

Home Forums Add-ons Options Page Copy options page to new post after selecting category

Solved

Copy options page to new post after selecting category

  • I want to copy global data from the options ACF fields to the ACF fields of a weather forecast, but only when I check the correct category (8).
    I tried the following code but this only works after publishing the message. I want that data already there when I check the category 8 (weather chats).
    Any idea how to handle this? (Ajax?)

    This is the code that I want to execute:
    function copy_weatherreport () {
    if (in_category (8)): // Category 8 are the Weather Talks
    $variable = get_field (‘general_air pressure’, ‘option’); update_field (‘general_air pressure’, $variable);
    endif;
    return;
    }
    add_filter (‘wp_insert_post’, ‘copy_weatherforecast’);

  • is “8” the term ID of the category?

    
    //https://www.advancedcustomfields.com/resources/acf-update_value/
    add_filter('acf/update_value/field=general_air_pressure', 'copy_pressure_to_cat', 20, 3);
    function copy_pressure_to_cat($value, $post_id, $field) {
      if ($post_id == 'options') {
        update_field('general_air_pressure', $value, 'term_8');
      }
      return $value;
    }
    
  • Hi, thanks!

    8 is indeed the category term. But what I’m mainly looking for is the hook that I have to use when the user selects that category 8.
    So: the user creates a new message, selects category 8 and WordPress immediately copies the ACF fields from the options to the ACF fields of this new message. They must also be shown immediately.

  • There is not hook that will automatically copy they message to the category when it is selected. That would require creating a script that would detect the change and make an AJAX request. In order to get the change to the category the options page would need to be saved.

    You can alter the term that the message is saved to by getting the field that selects the category and using that term ID in place of were I put the “8” in my code.

    
    update_field('general_air_pressure', $value, 'term_'.$term_id);
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Copy options page to new post after selecting category’ is closed to new replies.