Forked from chodorowicz/get_translated_media_or_original.php
Created
March 30, 2017 08:50
-
-
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
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
<?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