Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create permalink structure URLs for custom post types that include all parent terms from a custom taxonomy
<?php
/*
Term Archive Pages:
- http://example.com/recipes/dinner/
- http://example.com/recipes/breakfast,brunch/
Single Recipe Pages:
- http://example.com/recipes/dinner/soup-title/
- http://example.com/recipes/breakfast,brunch/egg-dish-title/
*/
add_action( 'init', 'register_my_types' );
function register_my_types() {
register_post_type( 'recipes',
array(
'labels' => array(
'name' => __( 'Recipes' ),
'singular_name' => __( 'Recipee' )
),
'public' => true,
'has_archive' => true,
)
);
register_taxonomy( 'occasion', array( 'recipes' ), array(
'hierarchical' => true,
'label' => 'Occasions'
)
);
}
// Add our custom permastructures for custom taxonomy and post
add_action( 'wp_loaded', 'add_clinic_permastructure' );
function add_clinic_permastructure() {
global $wp_rewrite;
add_permastruct( 'occasion', 'recipes/%occasion%', false );
add_permastruct( 'recipes', 'recipes/%occasion%/%recipes%', false );
}
// Make sure that all links on the site, include the related texonomy terms
add_filter( 'post_type_link', 'recipe_permalinks', 10, 2 );
function recipe_permalinks( $permalink, $post ) {
if ( $post->post_type !== 'recipes' )
return $permalink;
$terms = get_the_terms( $post->ID, 'occasion' );
if ( ! $terms )
return str_replace( '%occasion%/', '', $permalink );
$post_terms = array();
foreach ( $terms as $term )
$post_terms[] = $term->slug;
return str_replace( '%occasion%', implode( ',', $post_terms ) , $permalink );
}
// Make sure that all term links include their parents in the permalinks
add_filter( 'term_link', 'add_term_parents_to_permalinks', 10, 2 );
function add_term_parents_to_permalinks( $permalink, $term ) {
$term_parents = get_term_parents( $term );
foreach ( $term_parents as $term_parent )
$permlink = str_replace( $term->slug, $term_parent->slug . ',' . $term->slug, $permalink );
return $permlink;
}
// Helper function to get all parents of a term
function get_term_parents( $term, &$parents = array() ) {
$parent = get_term( $term->parent, $term->taxonomy );
if ( is_wp_error( $parent ) )
return $parents;
$parents[] = $parent;
if ( $parent->parent )
get_term_parents( $parent, $parents );
return $parents;
}
@asmerkin

This comment has been minimized.

Copy link

commented Dec 18, 2013

Gracias!. Esto me ha sido muy util!

Thank you!! This has been very useful to me!

@kvvaradha

This comment has been minimized.

Copy link

commented Mar 5, 2014

@fwolfers

This comment has been minimized.

Copy link

commented Mar 11, 2014

is it possible to get the taxonomy archive pagination to work.. gives me an 404 error when going to --> example: /recipes/dinner/page/2/

@talha-presstigers

This comment has been minimized.

Copy link

commented Oct 26, 2015

I want the permalink structure like this one below
www.site.com/post-type/year/post-name
I have used the code below to rewrite custom post slug, but it ends up with this "news/%year%/one/
where news = post type
one = post name
add_action( 'wp_loaded', 'add_clinic_permastructure' );
function add_clinic_permastructure() {
global $wp_rewrite;
add_permastruct( 'news', 'news/%year%/%news%', false );
$wp_rewrite->flush_rules();
}

@Nerdcomputers

This comment has been minimized.

Copy link

commented Mar 28, 2016

@magarampage

This comment has been minimized.

Copy link

commented Jun 25, 2016

Brilliant , big thanks man

@scottwvw

This comment has been minimized.

Copy link

commented Mar 22, 2017

This is great! Exactly what I needed!

I'm trying to get the posts to order by title on the Taxonomy page and not having much luck, is there an easy way of doing this I've missed?

Thanks

@gbiorczyk

This comment has been minimized.

Copy link

commented Oct 4, 2017

Hi,

If anyone still has a problem with the link structure in WordPress, then I would like to present you a plugin that solves this problem:

https://wordpress.org/plugins/wp-better-permalinks/

This plugin allows you do what with the code above and more - and all this with a few clicks in the admin panel. By using it you can achieve a friendly link structure:

Custom Post Type> Single Term> Post
Custom Post Type> Post (if there is no selected category)
Custom Post Type> Single Term

I invite you to check my plugin and if you like it - to recommend to others. I will be very grateful!

@Nsokyi

This comment has been minimized.

Copy link

commented Oct 12, 2017

This is exactly what I have been looking for, it works great for me but I do get the following error message 'Undefined variable: permlink in /Users/...' 'on line 89'.

@uniterrenerina

This comment has been minimized.

Copy link

commented Dec 26, 2017

after adding $wp_rewrite->flush_rules(); in wp_load action 404 page is appeare how to solve that .

@ghost

This comment has been minimized.

Copy link

commented Mar 15, 2018

Great Work, i solved a little problem adding a line here:

add_filter( 'term_link', 'add_term_parents_to_permalinks', 10, 2 );
function add_term_parents_to_permalinks( $permalink, $term ) {
$term_parents = get_term_parents( $term );
$permlink = '';
foreach ( $term_parents as $term_parent )
$permlink = str_replace( $term->slug, $term_parent->slug . ',' . $term->slug, $permalink );
return $permlink;
}

because without this i retrieve an error when i navigate custom taxonomy archive.

And i have a question:
i have a problem with wpml. When i try to change language in custom taxonomy archive it doesn't work, anyone knows how to solve that?

@orest22

This comment has been minimized.

Copy link

commented Apr 6, 2018

Note: taxonomy has to be registered before post type.

@aktarujjman

This comment has been minimized.

Copy link

commented May 8, 2018

Great Works

@nejehh

This comment has been minimized.

Copy link

commented May 29, 2018

how to replace , by /

@VHudzhal

This comment has been minimized.

Copy link

commented Oct 2, 2018

Hi! Thank for the example! But can somebody help me to do for this example own structure? But only for the three taxonomies: www.mysite/type-deal/type-realty/city/address. In my situation address is post name for wordpress.

@Zexccerd

This comment has been minimized.

Copy link

commented Dec 7, 2018

i have a post type with 2 different categories
how do i set up my permalink like this:
mysite.com/post_type/taxonomy_name/term/sub_term/post

@braddalton

This comment has been minimized.

Copy link

commented May 26, 2019

i have a post type with 2 different categories
how do i set up my permalink like this:
mysite.com/post_type/taxonomy_name/term/sub_term/post

I wouldn't use category and tags with CPT's.

Better to register custom taxonomy terms and separate standard posts from custom post types.

@john-ampwebdesign

This comment has been minimized.

Copy link

commented Jul 15, 2019

Thank you for the example. At least it got me on the right track and I didn't miss anything.
All to fix one link... Doh!

@mykhaylopetrov

This comment has been minimized.

Copy link

commented Aug 21, 2019

Hi.

If I want to add a non-hierarchical taxonomy like WordPress tags to your code.
For example:

register_taxonomy ( 'taggs', array ('recipes'), array (
        'hierarchical' => false,
        'label' => 'Taggs'
    )
);

As a result, the link structure will be as follows:

http://example.com/taggs/tag-1,tag-2,tag-3

How else to add “recipes” to this taxonomy so that:

http://example.com/recipes/taggs/tag-1,tag-2,tag-3

Tried like this:

function add_clinic_permastructure () {
    global $ wp_rewrite;
    add_permastruct ('occasion', 'recipes/%occasion%', false);
    add_permastruct ('recipes', 'recipes/%occasion%/%recipes%', false);

    add_permastruct ('taggs', 'recipes/%taggs%', false);
    add_permastruct ('recipes', 'recipes/%taggs%/%recipes%', false);
}

In this case, the structure http://example.com/recipes/dinner,breakfast,brunch does not work (error 404).

How to tie these 2 taxonomies together?

Thank.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.