wordpress-seo icon indicating copy to clipboard operation
wordpress-seo copied to clipboard

set_post_thumbnail no more updates the og:image meta tag

Open iamatef opened this issue 1 year ago • 3 comments

  • [x] I've read and understood the contribution guidelines.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Please give us a description of what happened

If a post was created programmatically then assigned a featured image using the function set_post_thumbnail, the generated post does not contain the og:image tag

To Reproduce

Step-by-step reproduction instructions

  1. Only have Yoast plugin enabled
  2. Run this test code that generates a post and set the featured image for the post
<?php

//wp-load.php is located in the root of the WordPress installation three levels up
require_once( '../../../wp-load.php' );

//thumbnail_id copy the ID of any media file from the media library
$thumbnail_id = 89050;

//insert a new post 
$my_post = array(
    'post_title' => 'My post',
    'post_content' => 'This is my post.',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_category' => array(8,39)
);

// Insert the post into the database
$pid = wp_insert_post( $my_post );

//set post thumbnail to 89050
set_post_thumbnail($pid, $thumbnail_id);

//post URL
echo get_permalink( $pid );
 

Expected results

  1. When viewing the source HTML of the generated post to find an og:image meta tag holding the thumbnail URL

Actual results

  1. The og:image tag is missing and the post must be updated for the og:image tag to appear

iamatef avatar Mar 09 '24 16:03 iamatef

Hi @iamatef

Thanks for using the Yoast SEO plugin and for creating the issue. I tried the code you shared, but it is not working at my end. So, please share the complete code to check this further.

Also, have you tried to edit the post and tried to click on the "Update" button without making any changes to check whether it helps or not?

We look forward to hearing from you and are happy to help you.

josevarghese avatar Mar 21 '24 18:03 josevarghese

Hi @josevarghese

  • For the code to work, make sure to modify it
  1. make sure WordPress wp-load file is correct on the test file that you are using, In my example the test.php file was located in /wp-content/plugins/plugin-name-directory so It was three levels up

require_once( '../../../wp-load.php' );

  1. make sure you have modified this line to have any ID of the media library thumbnails $thumbnail_id = 89050;

  2. The issue is about not having og:image for pragmatically inserted posts and there is no issue when adding the post manually or when clicking the Update button

Regards, Atef

iamatef avatar Mar 21 '24 23:03 iamatef

I was unable to reproduce the issue with the following code.

//wp-load.php is located in the root of the WordPress installation three levels up
require_once( ABSPATH . 'wp-load.php' );

//thumbnail_id copy the ID of any media file from the media library
$thumbnail_id = 1045; // unicorn-wallpaper.jpg . 89050 in comments below

//insert a new post 
$test_number = '1';
$my_post = array(
    'post_title' => 'My post #' . $test_number,
    'post_content' => 'This is my post.',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_category' => array(8,39)
);

// Insert the post into the database
$pid = wp_insert_post( $my_post );

//set post thumbnail to 89050
set_post_thumbnail($pid, $thumbnail_id);

//post URL
echo get_permalink( $pid );

The post was generated (a couple errors due to missing information on the new post - feeds, etc). The sitemap contains the new post and an image. Loading the post from the sitemap and viewing the source shows an OG image.

screenshot_134

Yoast has some limitations regarding images. Does the 89050 image work when creating a draft post under Admin > Posts? The image I used came from the Theme Unit Test. Can you reproduce the issue in a test environment with only the Theme Unit Test import and Yoast SEO? Can you think of anything that might help us reproduce the issue?

amboutwe avatar Mar 26 '24 18:03 amboutwe

Closing this issue due to inactivity. If the issue persists, please provide additional information so that we can investigate it further.

josevarghese avatar May 22 '24 11:05 josevarghese