Skip to content

Instantly share code, notes, and snippets.

@braddalton
Created May 24, 2019 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braddalton/946a4a530849c89f7779378f5434e512 to your computer and use it in GitHub Desktop.
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
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