What’s the Simplest Way to Override/Rewrite the %category% Permalink Structure Tag?

See: https://wordpress.org/support/article/using-permalinks/

By default, when someone sets their permalink structure to:

/%category%/%postname%/

Post URLs appear like so:

https://example.com/**abc**/some-cool-post/

ABC merely represents the first category of potentially several selected for the post, but unlikely the most appropriate.

I’ve made it so that my writers can select which category should be most prominent, which is saved as post meta for each post, and can be output with the variable $prominent_category.

I would like to automatically replace %category% with $prominent_category in the slug, without the need of registering an additional taxonomy or doing anything else that might be overkill, so that whatever the author sets as their prominent category, say Pizza, that the new structure for the post appears as so:

https://example.com/**pizza**/some-cool-post/

Are either category_rewrite_rules or post_link_category the right choice, or should I be going down a different path?


if ( $prominent_category = get_post_meta( get_the_ID(), 'prominent_cat', true ) ) {
echo esc_html( str_replace( ' ', '-', strtolower( $prominent_category ) ) );
}

Full attempt:

add_filter( 'post_link_category', 'prominent_link_category', 10, 3 );
function prominent_link_category( $cat, $cats, $post ) {
if ( $prominent_primary_cat = get_post_meta( get_the_ID(), 'prominent_primary', true ) ) {
$cat = get_term_by( 'name', esc_html( str_replace( ' ', '-', strtolower( $prominent_primary_cat ) ) ), 'category' );
} else {
$category = get_the_category();
$cat = esc_attr( $category[0]->cat_slug );
}
return $cat;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Yes, the post_link_category hook is indeed what you would use to filter the %category% replacement value in the permalink.

And here’s an example with prominent_cat being the meta key and the value being the category name (e.g. Pizza Hut), where I’m using get_term_by() to get the category object/data:

add_filter( 'post_link_category', 'my_post_link_category', 10, 3 );
function my_post_link_category( $cat, $cats, $post ) {
    $prominent_category = get_post_meta( $post->ID, 'prominent_cat', true );
    if ( $term = get_term_by( 'name', $prominent_category, 'category' ) ) {
        $cat = $term;
    }

    return $cat;
}

If the meta value is a term/category ID, then you can simply use get_term() to get the category data:

add_filter( 'post_link_category', 'my_post_link_category', 10, 3 );
function my_post_link_category( $cat, $cats, $post ) {
    $prominent_category = get_post_meta( $post->ID, 'prominent_cat', true );
    if ( $prominent_category && term_exists( (int) $prominent_category ) ) {
        $cat = get_term( $prominent_category );
    }

    return $cat;
}


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x