Support

Account

Home Forums General Issues Default values for sub field: Post Object

Solved

Default values for sub field: Post Object

  • I have a repeater field with 2 sub fields:
    GAS NAME (Post object field from Custom Post Type: Gas)
    GAS VALUE (number field)

    Below code can set default values for sub fields within a repeater field. The GAS VALUE (set to 0 in below example) works but this code doesn’t work for the GAS NAME, probably because its a Post Object field.

    Anyone knows how to set default values for a Post Object sub field?

    Thanks!

    
    add_filter('acf/load_value/key=field_5e23ef1222ea5',  'yl_pre_load_default_gasses', 10, 3);
    function yl_pre_load_default_gasses($value, $post_id, $field) {
    
      if ($value === false) {
    
        $value = array();
    
        $value[] = array(
          'field_5e23ef3a22ea6' => 'Title 1', // Post object field (CUSTOM POST TYPE: GAS)
          'field_5e23f00922ea7' => '0'
        );
        $value[] = array(
          'field_5e23ef3a22ea6' => 'Title 2', // Post object field (CUSTOM POST TYPE: GAS)
          'field_5e23f00922ea7' => '0'
        );
      }
    
      return $value;
    }
    
  • I’m not sure if this is helpful to you, but I just got a similar filter to work when the Post Object sub-field of the Repeater has the Return Format set to “Post ID”. With this setting, I just passed in the appropriate post_id as the value in the filter. So with your code above it’d be something like:

    if ($value === false) {
    
        $value = array();
        $post_id_1 = 28;
        $post_id_2 = 24;
      
        $value[] = array(
          'field_5e23ef3a22ea6' => $post_id_1, // post_id of the 1st Gas post that you want to choose
          'field_5e23f00922ea7' => '0'
        );
        $value[] = array(
          'field_5e23ef3a22ea6' => $post_id_2, // post_id of the 2nd Gas post that you want to choose
          'field_5e23f00922ea7' => '0'
        );
      }
    
      return $value;
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.