Skip to content

Instantly share code, notes, and snippets.

@Musilda
Created December 15, 2020 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Musilda/3ec510cc12607ec137b0e0e514c4cf6e to your computer and use it in GitHub Desktop.
Save Musilda/3ec510cc12607ec137b0e0e514c4cf6e to your computer and use it in GitHub Desktop.
<?php
global $wpdb;
$posts = $wpdb->get_results(
"SELECT wposts.*
FROM ".$wpdb->prefix."posts wposts, ".$wpdb->prefix."icl_translations wicl_translations
WHERE wicl_translations.element_id = wposts.ID
AND wposts.post_status = 'publish'
AND wicl_translations.language_code = 'en'
AND wicl_translations.element_type = 'post_post'
AND (wposts.post_type = 'post')"
);
foreach( $posts as $item ){
$thumb_id = get_post_meta( $item->ID, '_thumbnail_id', true );
$en_translation = $wpdb->get_results( "SELECT trid FROM ".$wpdb->prefix."icl_translations WHERE element_id = '" . $item->ID . "'" );
$cs_translation = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."icl_translations WHERE trid = '" . $en_translation[0]->trid . "' AND language_code = 'cs'" );
$de_translation = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."icl_translations WHERE trid = '" . $en_translation[0]->trid . "' AND language_code = 'de'" );
$data = $wpdb->get_results( "SELECT trid FROM ".$wpdb->prefix."icl_translations WHERE element_id = '" . $thumb_id . "'" );
if ( !empty( $data ) ) {
$result = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."icl_translations WHERE trid = '" . $data[0]->trid . "'" );
if ( !empty( $result ) ) {
foreach( $result as $image ) {
if ( 'cs' == $image->language_code ) {
update_post_meta( $cs_translation[0]->element_id, '_thumbnail_id', $image->element_id );
}
if ( 'de' == $image->language_code ) {
update_post_meta( $de_translation[0]->element_id, '_thumbnail_id', $image->element_id );
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment