Fix WPML integration for Cloudinary media inserted prior to WPML activation#1159
Open
gabrielcld2 wants to merge 2 commits intodevelopfrom
Open
Fix WPML integration for Cloudinary media inserted prior to WPML activation#1159gabrielcld2 wants to merge 2 commits intodevelopfrom
gabrielcld2 wants to merge 2 commits intodevelopfrom
Conversation
PatelUtkarsh
approved these changes
Mar 27, 2026
|
|
||
| // phpcs:ignore WordPress.DB | ||
| $sql = $wpdb->prepare( | ||
| "SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query})", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared |
There was a problem hiding this comment.
💬 suggestion: The query now searches for multiple contexts (e.g. en and default), but get_row() returns only one row. Without an ORDER BY, which row you get is up to MySQL. If a post has rows for both contexts, you want the current one (en) first and default only as a fallback.
Current code:
$sql = $wpdb->prepare(
"SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query})",
$this->post_id,
...$contexts
);
$data = $wpdb->get_row( $sql, ARRAY_A );Suggested fix:
$sql = $wpdb->prepare(
"SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query}) ORDER BY FIELD(`media_context`, %s) DESC LIMIT 1",
$this->post_id,
...$contexts,
$this->context
);
$data = $wpdb->get_row( $sql, ARRAY_A );FIELD(media_context, %s) scores 1 for an exact match on the current context and 0 for everything else. DESC puts the exact match first.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The issue
When Cloudinary is enabled without WPML,
cloudinary_relationshipsentries are inserted with amedia_contextset todefault.Once WPML is activated though, new
cloudinary_relationshipsentries have theirmedia_contextset to the current language (e.g.enfor English).Furthermore, when posts are looking for the relationships data for specific images, it currently attempts to fetch them using the current language for context.
This works well in the case where WPML was activated prior to Cloudinary, however creates some issues when activation is done the other way around:
enand therefore won't find the corresponding cloudinary_relationships entries, resulting in not found assetsApproach
QA notes