1

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;
}
2
  • 1
    Is $prominent_category a global variable; if so, where/when/how is it defined? Or is prominent_category the meta key? But yes, you'd want to use the post_link_category hook to filter the %category% replacement value.
    – Sally CJ
    Commented Dec 31, 2020 at 10:11
  • So does the metadata prominent_cat contain the category name (e.g. foo cat), slug (e.g. foo-cat) or ID (e.g. 123)?
    – Sally CJ
    Commented Jan 1, 2021 at 1:13

1 Answer 1

2

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;
}
1
  • Try again with my first example: copy the exact code, paste it into the theme functions.php file, and change the meta key (I see you used pc_primary in the question). Also, yes, the else part isn't needed since WordPress already did that which the value is the one you're changing/filtering. Additionally, instead of get_the_ID(), you should use $post->ID.
    – Sally CJ
    Commented Jan 1, 2021 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.