Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created July 21, 2017 23:07
Show Gist options
  • Save tzkmx/6d07e8e3081f317871eec83f59b4ff9a to your computer and use it in GitHub Desktop.
Save tzkmx/6d07e8e3081f317871eec83f59b4ff9a to your computer and use it in GitHub Desktop.
Complex query to make the og:image set by Yoast SEO,a custom profile thumb_id set in a custom post type
<?php
add_filter('option_wpseo_social', function( $opt ) {
if(is_author() ) {
$userid = get_queried_object_id();
$profile_thumb = get_profile_og_image_id( $userid );
if( $profile_thumb ) {
$timb = new \TimberImage( $profile_thumb );
$img_profile = \Timber\ImageHelper::resize($timb->src, 200, 200);
$opt['og_default_image'] = $img_profile;
}
}
return $opt;
});
function get_profile_og_image_id( $user_id ) {
$cached_profile = get_transient('profile_image_id_' . $user_id);
error_log('cached data: ' . $cached_profile . ' FOR user : ' . $user_id, 0);
if(!$cached_profile) {
global $wpdb;
$query = $wpdb->prepare("SELECT MM.meta_value AS profile_thumb_id " .
"FROM $wpdb->postmeta MM INNER JOIN " .
"( SELECT P.ID, meta_key, meta_value " .
" FROM $wpdb->posts P " .
" INNER JOIN $wpdb->postmeta M ON P.ID = M.post_id " .
" INNER JOIN $wpdb->users U ON M.meta_value = U.ID " .
" WHERE P.post_type = 'colaborador' " .
" AND M.meta_key = 'usuario' AND U.ID = %d ) ".
"PC ON MM.post_id = PC.ID WHERE MM.meta_key = '_og_image_thumbnail_id'", $user_id);
$profile_id = $wpdb->get_var($query, 0, 0);
error_log('retrieved data: ' . $profile_id . ' FOR user : ' . $user_id, 0);
$save= set_transient('profile_image_id_' . $user_id, $profile_id, 3600);
if($save) { $cached_profile = $profile_id ; }
}
return $cached_profile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment