Reputation: 1567
I've seen this asked all over the internet, but so far nothing has worked for me.
Ideally, I'd like the following to work:
http://example.com/mortgage-brokers
< taxonomy archive
http://example.com/mortgage-brokers/victoria
< taxonomy term
http://example.com/mortgage-brokers/victoria/melbourne
< second-level taxonomy term
http://example.com/mortgage-brokers/victoria/melbourne/john-smith
< single 'post'
I have the 'Custom post type permalinks' plugin set to /%broker_locations%/%postname%/
, and I've defined my CPT & taxonomy as follows:
$args = array(
'label' => __('Brokers'),
'singular_label' => __('Broker'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => true,
'rewrite' => [
'hierarchical' => false,
'slug' => 'mortgage-brokers',
'with_front' => false
],
'supports' => array('title', 'editor', 'thumbnail', 'tag', 'comments'),
);
register_post_type( 'broker' , $args );
register_taxonomy(
'broker_locations',
'broker',
array(
'hierarchical' => true,
'label' => 'Broker locations',
'hierarchical' => true,
'rewrite' => array(
'hierarchical' => true,
'with_front' => false
)
)
);
With the above configuration I can get post permalinks to work, as long I don't add a slug to 'rewrite' in the taxonomy definition, resulting in 'broker_locations' in the slug, i.e:
http://example.com/mortgage-brokers/victoria/melbourne/john-smith
< works
http://example.com/mortgage-brokers/victoria/melbourne
< 404s
http://example.com/mortgage-brokers/broker_locations/victoria/melbourne
< "works", but isn't desired
I've also been able to get either nested taxonomy archives OR post permalinks to work fine (depending on which one's defined first in functions.php), but the other one always 404s.
Does anyone have any idea what's going on? I can't believe how difficult something so simple has been.
Upvotes: 0
Views: 2019
Reputation: 187
To solve this, you need to clearly define custom rewrite rules and ensure WordPress understands the hierarchy. Change your rewrite rule for the broker CPT. This allows WordPress to recognize %broker_locations% as a placeholder for the taxonomy.
$args = array(
'label' => __('Brokers'),
'singular_label' => __('Broker'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => true,
'rewrite' => array(
'slug' => 'mortgage-brokers/%broker_locations%',
'with_front' => false,
'hierarchical' => true
),
'supports' => array('title', 'editor', 'thumbnail', 'tag', 'comments'),
);
register_post_type( 'broker' , $args );
Upvotes: -1
Reputation: 1567
If anyone's stumbling across this, I've found my answer: https://wordpress.stackexchange.com/questions/196797/add-taxonomy-in-custom-permalink-structure/196799
'AddWeb Solution Pvt Ltd' in that thread has written their own function that'll fix this. It's somewhat manual in that you have to specify how many levels deep your post is, but it certainly did the trick for me.
Upvotes: 1