Created
April 24, 2018 22:17
-
-
Save danielbachhuber/f4b65c317b1e1d11a28bfffa173704d1 to your computer and use it in GitHub Desktop.
Use term ids for tag links
This file contains 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 | |
/** | |
* Transform 'tag=' (which expects slug) to 'tag_id=' in rewrite rules. | |
*/ | |
add_filter( 'post_tag_rewrite_rules', function( $rewrite_rules ) { | |
$processed_rules = array(); | |
foreach ( $rewrite_rules as $match => $query ) { | |
$processed_rules[ $match ] = str_replace( 'tag=', 'tag_id=', $query ); | |
} | |
return $processed_rules; | |
}); | |
/** | |
* 'tag_id' is normally a private query variable, and we need to make it public. | |
*/ | |
add_filter( 'query_vars', function( $query_vars ){ | |
$query_vars[] = 'tag_id'; | |
return $query_vars; | |
}); | |
/** | |
* Transform tag links to use term id instead of slug. | |
*/ | |
add_filter( 'term_link', function( $link, $term, $taxonomy ) { | |
if ( 'post_tag' !== $taxonomy ) { | |
return $link; | |
} | |
return home_url( 'tag/' . $term->term_id . '/' ); | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment