Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tao-hpu/0a3b854993bbea4064db180ea0d19bd5 to your computer and use it in GitHub Desktop.
Save tao-hpu/0a3b854993bbea4064db180ea0d19bd5 to your computer and use it in GitHub Desktop.
get file media file in current language, if it doesn't exist get original version
<?php
if (!function_exists('get_translated_media_or_original')) {
function get_translated_media_or_original($media_file_id) {
global $wpdb;
// check language of media file
$custom_query = "SELECT * FROM ".$wpdb->postmeta." WHERE meta_key='wpml_media_lang' AND post_id=".$wpdb->escape($media_file_id);
$meta = $wpdb->get_results( $custom_query );
$media_file_language = $meta[0]->meta_value;
// if language of media file equals current language, return that id
if($media_file_language == ICL_LANGUAGE_CODE) {
return $media_file_id;
}
// else look for original of this duplicate
else {
$custom_query = "SELECT * FROM ".$wpdb->postmeta." WHERE meta_key='wpml_media_duplicate_of' AND meta_value=".$wpdb->escape($media_file_id);
$meta = $wpdb->get_results( $custom_query );
// return $meta;
if (is_array($meta) && !empty($meta) && isset($meta[0])) {
$meta = $meta[0];
}
if (is_object($meta)) {
return $meta->post_id;
}
else {
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment