Home Forums ACF PRO How to save ACF data to a unique post and user combination


How to save ACF data to a unique post and user combination

  • On a WordPress site I have authors who can submit content and editors who can evaluate and comment on it. Submitted content uses it’s own post type and evaluation is done showing a ACF 5 form with text and star rating fields.

    My problem is a conceptual one, namely where to save evaluation data to:

    1. I can’t save it to the post as there have to be evaluations of different editors.
    2. I can’t save it to the user (neither author nor editor) as authors can submit various contents and editors can evaluate different contents.

    So I would need to save the evaluations neither to submitted content (post_1, post_2, …) nor to evaluators (user_1, user_2, …) but to a unique post and user combination (evaluation_1_1, evaluation_1_2, …):

    conceptual outline

    How is the common way to do so? Do I have to create a new content type “evaluation” with references to submitted content and to evaluating editor and make queries on each evaluation to know if content already has been evaluated?

    Or is there an easier way to go? I think of something like a custom $post_id:

    'post_id' => "post_".$post_id."_".$uid,

  • Maybe real simply thought, but why not store the data in post_meta in an array ? Then it can hold several evaluations. It doesn’t have to be a single value.

  • Thanks for your thought. Can ACF store data in post_meta or would I have to do that directly with WordPress API?

    Could you outline the steps to follow? I have some programming experiences but know little about ACF and WordPress API.

  • Edit: if the field is an ACF field, then you can. Then you can use update_field like this update_field($selector, $value, $post_id);

    You can save it like any other value except now you save an array.

    $your_array = array( 'some value', 'other value' );
    update_post_meta( $post_id, $meta_key, $your_array );
    update_field( $meta_key, $your_array, $post_id );
  • Thanks for your hint, saving evaluation data as array solved my issue.

    I implemented this using a standard ACF form group loaded from the single.php template and adding the following hooks:

    1. Hook into acf/update_value and retrieve value:
      $value = get_post_meta( $post_id, $field['name']);
    2. In the same hook overwrite $value as an array:
      $value[get_current_user_id()] = $value;
    3. Hook into acf/load_field and retrieve value:
      $value = get_post_meta( get_the_ID(), $field['name']);
    4. In the same hook extract value from array:
      $field['value'] = $value[0][get_current_user_id()];
  • This reply has been marked as private.
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘How to save ACF data to a unique post and user combination’ is closed to new replies.