Skip to content

Instantly share code, notes, and snippets.

@lukewhitehouse
Created November 23, 2016 14:31
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 lukewhitehouse/c937d57309425b64cfe429626653a24f to your computer and use it in GitHub Desktop.
Save lukewhitehouse/c937d57309425b64cfe429626653a24f to your computer and use it in GitHub Desktop.
WordPress post type permalinks
<?php
/**
* Modify Work permalink to add in category
*/
function mixd_work_permalink_setup( $post_link, $id = 0, $leavename = FALSE ) {
// Test params are anywhere within the URL
if ( strpos('%work_type%', $post_link) === 0 ) {
return $post_link;
} else {
// Get the posts data
$post = get_post($id);
// If the $post data is an object and has a post_type of 'case-studies'
if ( !is_object($post) || $post->post_type != 'work' ) {
return $post_link;
} else {
// Get the $post's terms
$terms = wp_get_object_terms($post->ID, 'work_type');
// If there are no terms
if ( !$terms ) {
// change params to
return str_replace('%work_type%', '', $post_link);
} else {
// change params to term slug
return str_replace('%work_type%', $terms[0]->slug, $post_link);
}
}
}
}
add_filter('post_type_link', 'mixd_work_permalink_setup', 1, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment