Support

Account

Home Forums Add-ons Gallery Field Feature image from first gallery image Reply To: Feature image from first gallery image

  • "save_post_{$post_type}" runs before "save_post". ACF does not set the values of the field until the second hook. So you are trying to get values from a field that has not been set yet. When you update the post the second time (and after) you are using values that were saved the previous time the the post was saved and it will not reflect changes correctly if the first image in the gallery has been changed.

    Whenever dealing with ACF you need to use the acf/save_post hook. https://www.advancedcustomfields.com/resources/acf-save_post/

    I know that you are trying to limit your filter to a specific post type, but you can do this in your acf/save_post filter

    
    add_action( 'acf/save_post', 'set_featured_from_gallery', 20);
    set_featured_from_gallery ($post_id) {
      if (get_post_type($post_id) != 'galleries') {
        return;
      }
      // code continues...
    }