Set WordPress post thumbnail programmatically

/**
 * Sets the same thumbnail ID (`123`) for all posts without thumbnails, i.e., "image placeholder".
 *
 * @see https://wpcodebook.com/set-wordpress-post-thumbnail-programmatically/
 * @see https://developer.wordpress.org/reference/functions/get_post_thumbnail_id/
 */
add_filter( 'post_thumbnail_id', function ( $thumbnail_id, $post ) {
	return ( ! $thumbnail_id ? 123 : $thumbnail_id );
}, 10, 2 );

Leave a Comment