You can use a conditional block that checks whether the featured image is empty, and inside the block (for when the condition is met) add an image block to output your default image.
Outside of the conditional block you add an image block for the featured image, which won't output anything if there is no featured image.
You can see in the screenshot how it looks adding such a condition.
If you are not using blocks you will be doing the same but with shortcodes, and the condition for the conditional shortcode would look like...
// Inside your functions file add the following code
//
function tournoi_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID); // If post have a featured image use that.
if (!$already_has_thumb) {
// If post does not have a featured image then get the first post image and set as featured image.
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); // Number 1 relates to taking post image number 1 and adding it as a featured image.
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
//$attachment_id = attachment_url_to_postid( $image_url );
// echo $attachment_id;
}
}
else if (is_post_type('tournoi')) && ( has_term( 'football', 'sport-tournoi' ) ) {
set_post_thumbnail($post->ID, '1');
}
else if (is_post_type('tournoi')) && ( has_term( 'futsal', 'sport-tournoi' ) ) {
set_post_thumbnail($post->ID, '2');
}
else if (is_post_type('tournoi')) && ( has_term( 'basket', 'sport-tournoi' ) ) {
set_post_thumbnail($post->ID, '3');
}
else if (is_post_type('tournoi')) && ( has_term( 'handball', 'sport-tournoi' ) ) {
set_post_thumbnail($post->ID, '4');
}
else if (is_post_type('tournoi')) && ( has_term( 'rugby', 'sport-tournoi' ) ) {
set_post_thumbnail($post->ID, '5');
}
}
} //end function
add_action('the_post', 'tournoi_featured');
add_action('save_post', 'tournoi_featured');
add_action('draft_to_publish', 'tournoi_featured');
add_action('new_to_publish', 'tournoi_featured');
add_action('pending_to_publish', 'tournoi_featured');
add_action('future_to_publish', 'tournoi_featured');
Your custom codes will update the post feature image setting when edit/publish any post, it won't update existed posts.
You can test those custom codes, check if it works for you.
2) The custom codes you mentioned above is using yoast SEO filter hook "wpseo_opengraph_image", which is out the range of Toolset support.
And I have search it in Google, the filter hook "wpseo_opengraph_image" seems to be a filter hook for displaying an og:image tag in frontend, I don't think it is a good idea to use WP function set_post_thumbnail() to setup post featured image here, if you want to display a featured image from specific image, please try with function get_the_post_thumbnail_url(), see WP document: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/