Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Prevent specific categories from being used in URL/permalink WordPress

  • SOLVED

For posts using multiple categories, WordPress will create the permalink by selecting the category with the lowest category ID. I have certain categories that I dont' want to be used and would like a way to prevent them from being used in the permalink.

Don't want the solution to be a plugin or manual (tried that and editors screw up).
Ideally it would be code added to functions.php that will restrict categories by ID.

Answers (3)

2011-02-21

John Cotton answers:

Hey Tracy

I think what you're after happens in the get_sample_permalink function.

In which case, you could catch the new permalink with a filter and do your checks/changes.

add_filter('post_link', 'filter_permalink_categories', 10, 3);
function filter_permalink_categories($permalink, $post, $leavename) {
if($post->filter == 'sample') {
// At this point, the permalink looks like this:
// http://mywebsite.com/current-category-slug/%postname%

// So let's change!
$permalink = str_replace('current-category-slug', 'my-other-category-slug', $permalink);
}
return $permalink;
}


Obviously, your code would need to be more sophisticated at the str_replace line - testing for those you don't want and replacing with whatever you do, but hopefully you get the idea.

2011-02-22

Vidyut Kale answers:

uh, about my code above, you should use one variable for the unwanted cats and use it in the "in_category" as well. I didn't, above.... and I don't seem to have an option to edit it better.
Something like:

function choosy_cat_permalink() {
$old=get_permalink(); //mistake in earlier code
$old_cats=get_the_catefories();
//you can edit this array or rig up something in theme options
$dont_want_cats = array('all', 'unwanted', 'cat', slugs');
if ( in_category( $dont_want_cats )) {
$nowcat = $new_cats[0]->slug;
if ( $parent = $new_cats[0]->parent )
$nowcat = get_category_parents($parent, false, '/', true) . $category;
$final = str_replace($dont_want_cats, $nowcat, $old);
return $final;
}

In your template, you can call it like:

<a href="<?php choosy_cat_permalink(); ?>"> Your post title or <?php the_title(); ?></a>


Vidyut Kale comments:

I don't know how well this will work, but try:


function choosy_cat_permalink() {
$old=get_the_permalink();
if ( in_category( 'something' )) {
$old_cats=get_the_catefories();
$dont_want_cats = array('all', 'unwanted', 'cat', slugs');
$new_cats = array_diff( $old, $dont_want_cats );
$nowcat = $new_cats[0]->slug;
if ( $parent = $new_cats[0]->parent )
$nowcat = get_category_parents($parent, false, '/', true) . $category;

$final = str_replace($dont_want_cats, $nowcat, $old);

return $final;
}


I am in a hurry (going out), so I haven't really got the time to see if this code is actually working, but throwing it in, if it helps. If it doesn't, I'll come back and take a look tomorrow.

Essentially, what this should do is check to see if the post belongs in the unwanted categories and if it does, then it gets a new list of categories it belongs to, and assigns a new slug from it, and replaces it in the original string.

You'll have to call the new permalink function in your templates where you want to use these doctored permalinks - everywhere, or in specific places.


Vidyut Kale comments:


Vidyut Kale comments:


Vidyut Kale comments:

2011-02-21

Lew Ayotte answers:

You can do this several ways...

I'd probably create a category-ID.php file (where ID is the category you don't want to display). Then stick in some code to redirect it to a different category/page.

See:
http://codex.wordpress.org/Category_Templates#What_Template_File_is_Used.3F

Lew