Skip to content

Instantly share code, notes, and snippets.

@moxdev
Created February 13, 2019 21:21
Show Gist options
  • Select an option

  • Save moxdev/2150a0b0e23bcd30790f55ddd5fc693e to your computer and use it in GitHub Desktop.

Select an option

Save moxdev/2150a0b0e23bcd30790f55ddd5fc693e to your computer and use it in GitHub Desktop.
Custom Taxonomy URL Rewrite Rule #wp
add_action( 'init', 'custom_tax_rewrite_rule', 10, 0 );
/**
* Rewrites the Property Location's url to be '/$slug/'
* Example:
* http://quantum.test/index.php?property_locations=virginia rewrites to
* http://quantum.test/virginia/
*
* @return void
*/
function custom_tax_rewrite_rule() {
$cats = get_terms(
'property_locations', // edit this to be your custom taxonomy
array(
'hide_empty' => false,
)
);
if ( count( $cats ) ) {
foreach ( $cats as $cat ) {
add_rewrite_rule( $cat->slug . '/?$', 'index.php?property_locations=' . $cat->slug, 'top' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment