Support

Account

Home Forums General Issues Using first gallery image as the featured image… Reply To: Using first gallery image as the featured image…

  • Thanks for your reply John.

    I managed to make this work with the following code:

    add_filter('acf/save_post', 'gallery_to_thumbnail',11,11);
    function gallery_to_thumbnail($post_id) {
    $gallery = get_field('images', $post_id, false);
    var_dump($gallery);
    var_dump($post_id);
    
    if (!empty($gallery)) {
    
    $image_id = $gallery[0];
    
    set_post_thumbnail($post_id, $image_id);
    
    }
    

    I realised my issue isn’t with setting the featured image but setting the og:image based on the featured image for which I am using Yoast plugin. This only works once I update a post rather than create one.

    I know Yoast has a filter but I am unsure how to set it using the gallery field, any ideas would be greatly appreciated:

    add_filter('wpseo_opengraph_image', 'og_image');
     
    function og_image($image) {
        global $post;
     
        if (get_field('feature_image', $post->ID)) {
            $image = base_image_url(get_field('feature_image', $post->ID), null); 
        }
        return $image;
    }