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
- Prioritise Pages over Taxonomy Term Root Archive, but not Taxonomy Term Child Archives
- 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?
- Get Terms of custom Taxonomy of products with certain Product Category
- Problems using get_the_terms in a plugin
- How to add a shortcode function that returns the taxonomy slug of the actual post within the loop
- How to list custom taxonomy terms alphabetically by last word
- Tree view wp-admin terms
- Retrieve the child terms by having the parent’s information
- How can I rewrite the URL of my custom taxonomy to include the year?
- Infinite Scroll appending Next Product Category Term pages
- add pagination in wordpress page template
- How do I get the correct URL?
- Get custom taxonomy terms that contain posts that have another taxonomy term
- Remove custom taxonomy slug from URL
- Order get_terms by count using a custom taxonomy hierarchy
- How to replace custom post type with custom taxonmy in permalinks
- Listing Cities A custom taxonmy by selection order
- Cloning product not copying custom taxonomies
- Add A CSS Class To A Menu Item When A Custom Taxonomy Term is Present On Single Post
- How to create a non removable taxonomy term?
- Get taxonomy link with post type slug prefix
- Count tags for current post and save into custom meta field (and update it on post edit)
- Create new Taxonomy, add extra fields, register terms AND extra fields values?
- WordPress taxonomy terms archive template help
- wp_insert_post not adding taxonomy (using wp_set_object_terms)
- Filter “get_terms” query
- Show message if term slug changed
- get parent and childs from hierarchcial taxonomy
- How To Create Dynamic Permalink To Custom Taxonomy List of Current Posts?
- Add term to taxonomy programatically
- How to change custom categories term links?
- Restructuring permalink with more than one taxonomies
- generate random slug when adding taxonomy
- setting a custom post type taxonomy term by code
- Get Highest and Lowest get_term_meta() value from Taxonomy Term
- How to get a terms and posts associated with another term?
- Displaying subcategories and then posts in taxonomy template
- Why get_terms() behaves strangely when being called in admin (for use in meta box)?
- How do I check to see if a specific child term has shared posts with another set of child terms within the same taxonomy?
- Output slugs to use as class names for every taxonomy a post is attached to
- Print Custom Taxonomy Term Name
- Custom taxonomy return 404
- set_object_terms for custom taxonomy in custom post type – not working
- Group & Sort Taxonomy terms by letter – Is there a better way?
- Permalinks incorrectly inserting multiple categories
- qtranslate-x problem with custom term description
- 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?
- Orderby taxonomy term id using get_posts not working
- How to filter a taxonomy meta field to the ‘single_term_title’ filter hook
- Custom order of taxonomy using wp_get_object_terms and woocommerce_term_meta
- Custom post type term names with ampersand in the term name
- switch statement for taxonomy content
- WordPress Taxonomy Permalinks – Custom?
- Need help understanding a rewrite with multi-level taxonomy
- Sorting terms individually for each post
- List terms and order by second word
- Broken paths on taxonomies after changing permalink to post name
- Custom taxonomy rewrite in permalinks for posts gives 404 on pages
- If page is a taxonomy do X, if is a term, do Y
- Query pages by child term
- Getting the parent terms adds additional empty markup [closed]
- Display taxonomy term slugs
- How to retrieve the permalink for a specific (custom) term?
- Trying to Display Number of Posts in Term
- Remove Custom Taxonomy Slug only without removing Custom post type slug in permalinks
- Multiple taxonomies, what should the permalink look like?
- Custom taxonomy list in two columns
- Tax query array terms display out of order
- Order posts with custom taxonomy array
- Taxonomy to WordPress Permalinks of custom post type
- get_terms returns array starting at 4
- Taxonomy term breadcrumb; how?
- Determine if Term has Grandparent/Great-Grandparent
- How To Display Selected Terms For Custom Taxonomy?
- Insert term description programmatically into hierarchical custom taxonomy
- Adding a query var to taxonomy term archive – gets redirected to the other taxonomy archive page
- get multiple values from $_GET from multiple checkboxes
- displaying links if term is used
- Exclude 2 in 3 terms of A Taxonomy from all Archives
- How to include child terms within parent?
- add_query_arg to look up page title
- Display term description on hover using get_the_term_list
- stdClass::$labels /wp-includes/general-template.php undefined
- Get post terms with hierarchical relationships
- Permalinks not working on MU network with domain mapping
- Altering term_id and name via $wpdb class
- Multiple category lists on one page
- Unable to display the post titles in a drop down
- $term->taxonomy stripping out underscores
- Taxonomy archive link from term id
- How can I get parent term from a child term
- Create heirachy of post terms from array & assign post to terms
- get_terms() – unexpected ‘=>’ (T_DOUBLE_ARROW) error
- Display woocommerce product_cat child terms with thumbnails
- How can I get the term_id from the action hook ‘set_object_terms’?