fix: destination cards show a tour photo again

vc_term_image_url() cached empty results for 12h and relied on an SQL
meta comparison to find a tour with a photo; destination cards site-wide
(homepage and region pages) were blank. Read the photo in PHP from up to
10 tours (primary, else first gallery image), use a fresh cache key, and
expire empty results after an hour. Plugin 1.9.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 16:47:01 +03:30
parent 98e706477c
commit fd6dba3203
4 changed files with 21 additions and 15 deletions

View File

@@ -177,7 +177,9 @@ function vc_term_image_url( $term_id, $taxonomy = '' ) {
return $custom;
}
$cache_key = 'vc_term_img_' . $term_id;
// "img2": the v1 key cached empty results for 12h, which left cards blank
// long after their tours had photos. Empty results now expire in an hour.
$cache_key = 'vc_term_img2_' . $term_id;
$cached = get_transient( $cache_key );
if ( false !== $cached ) {
return $cached;
@@ -192,10 +194,14 @@ function vc_term_image_url( $term_id, $taxonomy = '' ) {
$url = '';
if ( $taxonomy ) {
// A few recent tours in the term; the first with any photo wins. The
// photo is read in PHP (primary, else first gallery image) — the same
// fallback the tour cards use — rather than filtered in SQL.
$query = new WP_Query(
array(
'post_type' => 'viator_tour',
'posts_per_page' => 1,
'post_status' => 'publish',
'posts_per_page' => 10,
'fields' => 'ids',
'no_found_rows' => true,
'update_post_term_cache' => false,
@@ -206,21 +212,21 @@ function vc_term_image_url( $term_id, $taxonomy = '' ) {
'terms' => $term_id,
),
),
'meta_query' => array(
array(
'key' => '_vc_primary_image_url',
'value' => '',
'compare' => '!=',
),
),
)
);
if ( ! empty( $query->posts ) ) {
$url = (string) get_post_meta( (int) $query->posts[0], '_vc_primary_image_url', true );
foreach ( $query->posts as $post_id ) {
$url = (string) get_post_meta( (int) $post_id, '_vc_primary_image_url', true );
if ( '' === $url ) {
$gallery = vc_json_decode_meta( get_post_meta( (int) $post_id, '_vc_gallery_image_urls', true ), array() );
$url = $gallery ? (string) reset( $gallery ) : '';
}
if ( '' !== $url ) {
break;
}
}
}
set_transient( $cache_key, $url, 12 * HOUR_IN_SECONDS );
set_transient( $cache_key, $url, '' !== $url ? 12 * HOUR_IN_SECONDS : HOUR_IN_SECONDS );
return $url;
}