Created
May 24, 2019 20:09
-
-
Save braddalton/946a4a530849c89f7779378f5434e512 to your computer and use it in GitHub Desktop.
Alternative Method for Using post_type_link Filter https://wp.me/p1lTu0-ghd
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
add_filter( 'post_type_link', 'my_custom_permalink', 10, 3 ); | |
function my_custom_permalink( $permalink, $post_id, $leavename ) { | |
if (( strpos( $permalink, '%country-type%' ) === FALSE ) && ( strpos($permalink, '%region-type%' ) === FALSE )) | |
return $permalink; | |
$post = get_post($post_id); | |
if (!$post) return $permalink; | |
$y_terms = wp_get_object_terms($post->ID, 'country-type'); | |
if (!is_wp_error($y_terms) && !empty($y_terms) && is_object($y_terms[0])) $y_taxonomy_slug = $y_terms[0]->slug; | |
else $y_taxonomy_slug = 'country'; | |
$m_terms = wp_get_object_terms($post->ID, 'region-type'); | |
if (!is_wp_error($m_terms) && !empty($m_terms) && is_object($m_terms[0])) $m_taxonomy_slug = $m_terms[0]->slug; | |
else $m_taxonomy_slug = 'region'; | |
return str_replace('%country-type%/%region-type%', $y_taxonomy_slug . '/' . $m_taxonomy_slug, $permalink); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment