I am currently trying to redirect a subdomain.blogger.com blog to a domain located on my hosted WordPress install. The code below is supposed to send a 301 redirect and then make that specific post appear on my blog but it appears to be directing the user only to the home page and not to the corresponding blog post.

/************** 301 REDIRECTION ***********************/
function labnol_blogger_query_vars_filter( $vars ) {
  $vars[] = "blogger";
  return $vars;

add_filter('query_vars', 'labnol_blogger_query_vars_filter');

 function labnol_blogger_template_redirect() {
  global $wp_query;
  $blogger = $wp_query->query_vars['blogger'];
  if ( isset ( $blogger ) ) {
   wp_redirect( labnol_get_wordpress_url ( $blogger ) , 301 );

 add_action( 'template_redirect', 'labnol_blogger_template_redirect' );

 function labnol_get_wordpress_url($blogger_slug) {
  global $wpdb;
  if ( preg_match('@^(?:https?://)?([^/]+)(.*)@i', $blogger_slug, $matches) ) {
   $q = "SELECT guid FROM $wpdb->posts LEFT JOIN $wpdb->postmeta
        ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
        WHERE $wpdb->postmeta.meta_key='blogger_permalink'
        AND $wpdb->postmeta.meta_value='$matches[2]'";
   $wp_url = $wpdb->get_var($q);
  return $wp_url ? $wp_url : home_url();
 /************** 301 REDIRECTION ***********************/

My Wordpress permalink structure is set to custom with the value:


Any help would be greatly appreciated.

1 Answer 1


Assuming that the url parsing is correct and the data is actually set up correctly in the DB I think your SQL query is wrong. My suggestion is that you replace it by calling wp_query() api with the relevant parameters.

Another suggestion that might not be a bug is not to use guid, as it is not a reliable source for a post address. It is better to get the post ID and use get_permalink() to get its url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.