title: WordPress: Set 'Featured image' as first ACF-gallery when saving post
body: <p>I found this function <a href="https://support.advancedcustomfields.com/forums/topic/display-1-gallery-picture-on-archive-page/" rel="nofollow noreferrer">here</a>.
  I'm using ACF pro.</p>
  
  <p>Update: I added variable according to comment below, this got rid of the error, however the function is still not working.</p>
  
  <p><strong>functions.php:</strong></p>
  
  <pre><code>add_action( 'save_post', 'set_featured_image_from_gallery' );
  
  function set_featured_image_from_gallery() {
    $post = get_post(); //Edit according to comment below      
    $has_thumbnail = get_the_post_thumbnail($post-&gt;ID);
  
    if ( !$has_thumbnail ) {
  
      $images = get_field('gallery', false, false);
      $image_id = $images[0];
  
      if ( $image_id ) {
        set_post_thumbnail( $post-&gt;ID, $image_id );
      }
    }
  }
  </code></pre>
  
  <p><strong>Error message when saving post (pressing "Update"-button):</strong></p>
  
  <p>Notice: Undefined variable: post in /Applications/MAMP/htdocs/pf-blank/wp/wp-content/themes/PF-Blank-theme/functions.php on line 600</p>
  
  <p>Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/pf-blank/wp/wp-content/themes/PF-Blank-theme/functions.php on line 600</p>
  
  <p>Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/pf-blank/wp/wp-content/themes/PF-Blank-theme/functions.php:600) in /Applications/MAMP/htdocs/pf-blank/wp/wp-admin/post.php on line 197</p>
  
  <p>Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/pf-blank/wp/wp-content/themes/PF-Blank-theme/functions.php:600) in /Applications/MAMP/htdocs/pf-blank/wp/wp-includes/pluggable.php on line 1174</p>