-
LindsayJane
I have added the php snippet below and it works well for a fallback image in an Element if a Featured Image is not selected. (Inline Background Image – {{featured_image key:url|featured_image_fallback}}).
I was wondering if I could add a conditional, so for a particular CPT (event) it could have a different fallback applied (whilst leaving the current fallback to apply elsewhere, as it does now).
add_filter('generateblocks_dynamic_tag_output', function ($output, $options) { $tag_name = $options['tag_name'] ?? ''; if ('featured_image' === $tag_name) { if (empty($output)) { return 'https://my-website.org.uk/wp-content/uploads/2025/10/my-image.jpg'; } } return $output; }, 10, 2);Thank you for any pointers.
-
Hi there,
Yes, you can modify your code like this:
add_filter('generateblocks_dynamic_tag_output', function ($output, $options) { $tag_name = $options['tag_name'] ?? ''; if ('featured_image' === $tag_name) { if (empty($output)) { if (is_singular('event')) { return 'url for event fallback featured image'; } return 'https://my-website.org.uk/wp-content/uploads/2025/10/my-image.jpg'; } } return $output; }, 10, 2);Let me know if this works!
-
LindsayJane
Perfect.
I amended slightly to include the Event Archive (just in case useful for someone else).
if (is_singular('event') || is_archive('event')) {Thank you.
-
You are welcome 🙂
-
LindsayJane
Quick update if anyone finds useful. My line
|| is_archive('event')didn’t seem to work, in that I found it applied it to various other non CPT archive pages.Updated to:
|| is_post_type_archive('event')is_archive(): This will be true on any archive page, including category archives, date archives, and CPT archives.
is_post_type_archive(): This function specifically determines whether the query is for an existing post type archive page.
Used without parameters, is_post_type_archive() returns true for any custom post type archive.
To check for a specific custom post type archive, you can pass the post type name as a parameter. -
Thanks for sharing and explaining 🙂
-
LindsayJane
Sorry to be back again.
I’ve discovered that when using a GB Post Query Loop for Post Type Events on a Page, it’s defaulting to the wrong fallback image, i.e. NOT to the:
if (is_singular('event') || is_post_type_archive('event')) { return 'https://my-website.org.uk/wp-content/uploads/2025/10/my-image.jpg'; }Is there anything I can add to catch this?
Thank you for your help. Lindsay
-
Try this PHP code instead, it adds a fallback image directly to the event post type instead of adding it based on the dynamic tag:
function add_fallback_featured_image( $value, $post_id, $meta_key, $single ) { // Only modify when retrieving the featured image if ( $meta_key === '_thumbnail_id' && empty( $value ) && !is_admin() ) { // Check if this is an 'event' post type if ( get_post_type( $post_id ) === 'event' ) { // Return your fallback image ID return 11; // <-- Use your fallback image ID (integer, not string) } } // Otherwise, return the original value return $value; } add_filter( 'get_post_metadata', 'add_fallback_featured_image', 50, 4 ); -
LindsayJane
Hi, thanks for the help.
Didn’t seem to work, if I remove my first code entirely where we were targeting the dynamic tag, I don’t get any image showing at all for all posts that don’t have a Featured Image.
Not sure if it makes any difference at all but I’m using a GB Image Block.
Maybe it’s not possible..?
Thanks again for any thoughts though, Lindsay
-
I updated the code https://generate.support/topic/fallback-image/#post-183267, and this time we use the image ID, so replace
11with the actual image ID that you can find in the URL when viewing the image via the media library. -
LindsayJane
Hi Ying
Thanks again for your help. Yes this worked, except it applied to Events even if they DID have a Featured Image – so all Events now have the fallback image returned.
Feel though it’s closer though 🙂
Thanks again so much, best wishes, Lindsay
-
Alvind
Hi Lindsay,
I’ve made a few adjustments to the snippet, can you give this version a try:
function add_fallback_featured_image( $value, $post_id, $meta_key, $single ) { // Only modify when retrieving the featured image if ( $meta_key === '_thumbnail_id' && !is_admin() ) { // Check if this is an 'event' post type if ( get_post_type( $post_id ) === 'event' ) { // IMPORTANT: Check if a featured image actually exists // We need to do a direct database check to avoid infinite loop global $wpdb; $actual_thumbnail = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = '_thumbnail_id'", $post_id )); // Only return fallback if no thumbnail exists in database if ( empty( $actual_thumbnail ) ) { return 11; // Your fallback image ID } } } // Return null to continue with normal metadata retrieval return $value; } add_filter( 'get_post_metadata', 'add_fallback_featured_image', 50, 4 );The original code was checking
empty($value), but$valueis null by default when the filter runs, regardless of whether a featured image exists or not. This caused it to always return the fallback image. -
LindsayJane
Hi Alvind
Brilliant. Seems to do the job.
I did think I’d be able to remove the code below from the original code – but I seem to have to retain this (which is fine – just thought I’d mention as it might help others).
if (is_singular('event') || is_post_type_archive('event')) { return 'https://my-website.org.uk/wp-content/uploads/2025/10/my-image.jpg'; }I’d never have got there without both Ying’s and your help. Phenomenal support.
-
Alvind
Glad to hear that! 🙂
- You must be logged in to reply to this topic.