12

I have searched the net with no success on what should be an easy thing.

I have a highly customized blog with pages, sub pages and posts. While I rely heavily on the use of categories and tags I don't want them to be viewable in a url. The same goes for authors and date categories. Basically I wan't to throw a 404 error if someone tries to access these pages. Can someone point me in the right direction? Thanks!

The following should not be accessible:

  • example.com/category/books/
  • example.com/tag/ebooks/
  • example.com/author/dickens/
  • example.com/2012/10/

2 Answers 2

9

building on chrisguitarguy's answer here is a quick snippet you can drop in your theme's functions.php file to do job

add_action('template_redirect', 'wpse69948_archive_disabler');
function wpse69948_archive_disabler()
{
    if(is_tag() || is_category() || is_date() || is_author())
    {
        global $wp_query;
        $wp_query->set_404();
    }
}
0
10

I wrote a plugin for this -- that needs some updating, but it should still work fine.

Essentially you hook in some place late after WordPress knows what page is requested (like template_redirect), use one of the conditional functions and either use $wp_query->set_404() or just redirect the user to a page of your choice.

To disable categories, tags, author and date archives:

<?php
add_action('template_redirect', 'wpse69948_tag_disabler');
function wpse69948_tag_disabler()
{
    if(is_tag() || is_category() || is_date() || is_author())
    {
        global $wp_query;
        $wp_query->set_404();
    }
}
3
  • Does the job perfectly! Thanks a lot, also for providing the useful link to the conditional functions!
    – psot
    Commented Oct 20, 2012 at 23:56
  • Seems kinda overkill to have this as a standalone plugin. Seems like most theme authors ought to be able to do this in functions.php or a custom plugin of their own.
    – serraosays
    Commented Oct 24, 2016 at 15:19
  • @chrisguitarguy, this stops the option to search by category e.g. &category_name. Is there a way around it?
    – Motivated
    Commented Jul 16, 2023 at 20:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.