I am developing a plugin for WordPress that dynamically adds virtual posts recalled from an external API to the post overview on the front page. The front page typically displays the post_title
, post_excerpt
, post_date
, author, and a thumbnail for each post in an overview.
My issue: While I am able to display the post_title
, post_excerpt
, and post_date
for the virtual posts, I am unable to inject the external thumbnail for the virtual posts. It seems WordPress requires the post to have an actual ID
and for the images to be uploaded to the WordPress media library for the thumbnail to work correctly (it obviously takes the basic post data and then looks for image data in the own database instead of taking the external link). However, I need to display external images as thumbnails without uploading them to the media library, as a dynamic upload would slow down the page load and copyright issues may arise.
What I’ve tried: Using add_filter(‘post_thumbnail_html’…) to override the thumbnail HTML:
add_filter('post_thumbnail_html', function ($html, $post_id, $post_thumbnail_id, $size, $attr) {
global $post;
if (isset($post->is_virtual) && $post->is_virtual) {
$html = '<img src="' . esc_url($post->primaryImage) . '" class="wp-post-image" />';
}
return $html;
}, 10, 5);
Updating the metadata of the virtual posts:
add_filter('get_post_metadata', function ($meta_value, $object_id, $meta_key, $single) {
global $post;
if (isset($post->ID) && $post->is_virtual && $meta_key === '_thumbnail_id') {
return $post->primaryImage;
}
return $meta_value;
}, 99, 4);
update_post_meta($post->ID, 'featured_image', $article['primaryImage']);
update_post_meta($post->ID, '_thumbnail_id', $article['primaryImage']);
update_post_meta($post->ID, 'custom_image_url', $article['primaryImage']);
None of these attempts have worked for displaying the external images as thumbnails in the post overview.
Is there any way to add a thumbnail to virtual posts using external images, without creating a post ID and uploading those images to the WordPress media library? How can I get WordPress to display the external image as the thumbnail for these virtual posts?