Redirect tag archives to specific pages?
-
Hi,
I came across this bit of code and am wondering what I’m doing wrong…add_action( 'template_redirect', 'my_redirect_term_to_post' ); function my_redirect_term_to_post() { global $wp_query; if ( is_tax() ) { $term = $wp_query->get_queried_object(); if ( 'posttag' == $term->taxonomy ) { $post_id = my_get_post_id_by_slug( $term->slug, 'post' ); if ( !empty( $post_id ) ) wp_redirect( get_permalink( $post_id ), 301 ); } } } function my_get_post_id_by_slug( $slug, $post_type ) { global $wpdb; $slug = rawurlencode( urldecode( $slug ) ); $slug = sanitize_title( basename( $slug ) ); $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $slug, $post_type ) ); if ( is_array( $post_id ) ) return $post_id[0]; elseif ( !empty( $post_id ) ); return $post_id; return false; }
Basically I want the user to click on the tag at the bottom of a post, and have them be redirected to a page that has the same slug as the tag itself. I added this code to m functions.php file but it’s not working and I’m a bit of a n00b!
- The topic ‘Redirect tag archives to specific pages?’ is closed to new replies.