As always it was too simple in the end, was using the wrong hook. post_link
is for posts only should use post_type_link
instead.
/**
* replace the '%item%' tag with the first
* term slug in the item taxonomy
*
* @wp-hook post_link
* @param string $permalink
* @param WP_Post $post
* @return string
*/
add_filter( 'post_type_link', 'wpse_56769_post_link', 10, 2 );
function wpse_56769_post_link( $permalink, $post ) {
if( $post->post_type == 'product' ) {
$default_term = 'no_item';
$terms = wp_get_post_terms( $post->ID, 'item' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) )
$term = current( $terms )->slug;
else
$term = $default_term;
$permalink = str_replace( '%item%', $term, $permalink );
}
return $permalink;
}
Related Posts:
- Permalink Rewrite to include Custom Taxonomy Term
- post_type_link not working for posts without terms
- Retrieve posts by term id custom query
- Change order of Custom Taxonomy List
- Query users by custom taxonomy and user role
- Determine Term depth
- Custom taxonomy listing page when no term set (all terms)
- Custom order of terms for custom taxonomy in admin and website
- Is there a way to disable a term rather than deleting it?
- Best pratice to make taxonomy terms translatable without changing slugs?
- Front End Post Save Child terms
- Taxonomy slug by term ID
- How to enable hierarchical permalinks for hierarchical taxonomies
- wp_insert_term auto unique name
- How can I set up a category “overview” page?
- wp_get_object_terms – How can I order the resulting array by hierarchy?
- Display Custom Taxonomy Terns ordered by meta_value
- Custom Taxonomy terms with latest post ordered by date pagination issue
- Hierarchical display of custom taxonomy
- Listing child terms of parent term
- Display the taxonomy name of the current term
- custom taxonomy archive permalink shows 404 error
- Advanced custom fields – taxonomy terms images [closed]
- List only first-level children of specific custom taxonomy term
- get_terms of post->id
- Change URL structure of subcategory archive pages
- get / list categories and counts filtered by custom taxonomy term
- How to get a list of taxonomy terms which are being used only within certain post types?
- WPML Translating a term/taxonomy programmatically
- Allow only 1 instance of each term in each custom taxonomy
- Keep taxonomy base name in post permalinks
- Outputting an array of term meta values in a custom WooCommerce tab?
- Get terms from posts submitted within a certain time
- Display Terms for all posts in Current Archive or Query
- get_terms: determine if taxonomy term has children
- Display post taxonomies tree
- get_term_children returns WP_Error for custom taxonomy
- Exclude Taxonomy Terms from Template Via Back End?
- Permalinks for custom taxonomy stopped to work
- Displaying terms by first letter
- Print terms with taxonomy and metabox value
- Get custom category image from menu
- Same taxonomy for different object types
- How to show all possible parents and children of a hierarchical taxonomy term?
- How do I modify taxonomy term slugs dynamically?
- Failure to add term meta
- Hierarchical Taxonomy Terms Select Menu Output with selected=”selected” Set
- Can’t set custom taxonomy terms via custom form
- get_terms not returning any custom taxonomies
- How to change url for taxonomy pages?
- Search Query for Multiple Terms In Same Taxonomy
- What does ‘category__in’ mean in queries?
- How to write sql query to get the posts from a custom taxonomy term name
- Get only immediate children (and not grandchildren) of a hierarchical custom taxonomy term
- How to get_the_term_list() from multiple taxonomies?
- How to group all terms children’s in custom taxonomy?
- Display Posts by Custom Taxonomy Chosen Terms
- How to display a custom taxonomy without a link?
- Build filter for post-type + multi taxonomies + multi terms
- How do I get term data associated with the current post, where the result is not wrapped in HTML?
- wp_dropdown_categories – how to save in widget?
- echo a specific custom taxonomy term on a different custom taxonomy archive
- How to show post list based on taxonomy term?
- WordPress modify Settings > Permalinks (options-permalink.php) Add CPT Permalink Options
- get_term_by works but get_terms doesn’t?
- Display empty custom taxonomies differently than with one with posts
- Double slash in the_terms URL
- How to call a custom taxonomy term name inside a function?
- How to get this value inside RETURN
- How to list Custom Taxonomy
- Custom Taxonomy List with Children
- How to output taxonomy term (incl. link to archive) on author.php?
- Page for custom taxonomy
- Display Child Categories of Current Post ID
- Set two terms for a post when they differ only by an accent
- How can I show links for custom posts group by taxonomy current term and sub-term?
- How to use custom slug and custom templates for custom taxonomy?
- How can I rewrite the URL of my custom taxonomy to include the year?
- Infinite Scroll appending Next Product Category Term pages
- Listing Cities A custom taxonmy by selection order
- get parent and childs from hierarchcial taxonomy
- Get Highest and Lowest get_term_meta() value from Taxonomy Term
- Print Custom Taxonomy Term Name
- Custom taxonomy return 404
- set_object_terms for custom taxonomy in custom post type – not working
- Permalinks incorrectly inserting multiple categories
- Resize $term attachment using url
- Product dimensions filter by taxonomy
- How can I pull the slug of a custom taxonomy and output it in a class?
- switch statement for taxonomy content
- How to retrieve the permalink for a specific (custom) term?
- Remove Custom Taxonomy Slug only without removing Custom post type slug in permalinks
- How To Display Selected Terms For Custom Taxonomy?
- Adding a query var to taxonomy term archive – gets redirected to the other taxonomy archive page
- displaying links if term is used
- How to include child terms within parent?
- stdClass::$labels /wp-includes/general-template.php undefined
- $term->taxonomy stripping out underscores
- How can I get parent term from a child term
- get_terms() – unexpected ‘=>’ (T_DOUBLE_ARROW) error