All we need is an easy explanation of the problem, so here it is.
The goal is to have a structure like this: example.com/tax_1/tax_2/post_name
This code produces the right structure but the post can’t be found (404)
// Register custom post types
function register_work() {
$args = array(
'public' => true,
'label' => 'Projekt',
'menu_name' => 'work',
'menu_icon' => 'dashicons-welcome-view-site',
'has_archive' => false,
'supports' => array( 'thumbnail', 'title' ),
'rewrite' => array(
'slug' => '%filter_1%/%filter_2%',
'with_front' => false
)
);
register_post_type( 'work', $args );
}
add_action( 'init', 'register_work' );
// Register tax
function work_tax() {
register_taxonomy(
'filter',
'work',
array(
'label' => __( 'Tags' ),
'hierarchical' => true,
'rewrite' => array(
'slug' => 'filter',
'with_front' => false
)
)
);
}
add_action( 'init', 'work_tax' );
// Replace permalink slug
function custom_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'work' ){
$terms = wp_get_object_terms( $post->ID, 'filter' );
if( $terms ){
$find = array('%filter_1%','%filter_2%');
$replace = array($terms[0]->slug,$terms[1]->slug);
return str_replace($find, $replace, $post_link);
}
}
return $post_link;
}
add_filter( 'post_type_link', 'custom_permalinks', 1, 2 );
I have regenerated the .htaccess file by saving the permalinks in the settings menu.
Does anyone know if it even possible to do this?
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Method 1
This code produces the right structure but the post can’t be found
(404)
That is because the custom rewrite tags (%filter_1%
and %filter_2%
) appeared as-is in the generated rewrite rules (in the database). So you need to register the tags so that they’re replaced with the proper RegEx (regular expression) pattern when WordPress (re-)generates the rules.
And you can register them using add_rewrite_tag()
like so:
function register_work() {
// Register the custom rewrite tags.
add_rewrite_tag( '%filter_1%', '([^/]+)' );
add_rewrite_tag( '%filter_2%', '([^/]+)' );
// ... then register the post type.
}
And that should fix the error, but you should know that your post type’s rewrite rules will result in issues like Pages (posts of the page
type) showing the homepage instead of the correct Page.
So you should just use a unique rewrite base, e.g. work
as in 'slug' => 'work/%filter_1%/%filter_2%'
to avoid clashes with other rewrite rules, particularly the default ones in WordPress.
However, if you really must use no rewrite base there, then there is a way.
-
But each time after you created or deleted a
filter
term, or maybe changed its slug, you need to manually flush the rewrite rules by simply visiting the permalink settings page.Note: Yes there is a programmatic way to automatically flush the rewrite rules, but doing it manually is really easy, so just create/edit/delete your
filter
terms in bulk and then manually flush the rewrite rules. 🙂 -
And there should be no posts (Pages, Posts, CPTs) or terms having the same slug as used by your
filter
terms. I.e. If you have afilter
term with the slugfoo
, then you should have no posts or terms (in other taxonomies) having the slugfoo
.But what I really mean is, you can use the same slug, but not in all cases and be wary. For example, if a
work
CPT post hasfoo/bar/baz
as the permalink, then there should be no Page with that path, i.e. a Page with the slugbaz
that’s a child of thebar
andfoo
Pages.
So if those are good to you, then here’s the code you can try:
add_filter( 'rewrite_rules_array', function ( $rules ) {
$terms = get_terms( [
'taxonomy' => 'filter',
'hide_empty' => false,
'fields' => 'slugs',
] );
if ( ! is_wp_error( $terms ) ) {
foreach ( $terms as $slug ) {
$match = "^{$slug}/(.*?)/([^/]+)(?:/(\d+)|)/?$";
$query = 'index.php?post_type=work&work=$matches[2]&name=$matches[2]&page=$matches[3]';
$rules[ $match ] = $query;
}
}
return $rules;
} );
And in addition to that code:
-
In
register_work()
, usework
as the base:'slug' => 'work/%filter_1%/%filter_2%',
-
Then in the
custom_permalinks()
function:// You need to replace this: $find = array('%filter_1%','%filter_2%'); // with this: $find = array('work/%filter_1%','%filter_2%');
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0