Remove Unwanted Archive URLs in WordPress

No doubt WordPress is most widely used blogging tool but it is also used to create websites without blog feature. Other than normal posts and pages URLs, wordpress also has unwanted archive URLs for categories, tags, dates and authors archives and a search URL. For example, open your wordpress site that does not have a blog and go to this URL YOUR SITE NAME/category/uncategorized/ and you might see a broken template or unwanted content. Similarly if you are not using search feature in your site and you go to this URL YOUR SITE NAME?s=foo You might again see something not desired. Following code snippet will redirect all categories, tags, dates, authors and search urls to 404 page. Paste it in your template’s functions.php file.

function ts_remove_unwanted_archives(){
  if( is_search() || is_category() || is_tag() || is_date() || is_author() ) {
    global $wp_query;
add_action('template_redirect', 'ts_remove_unwanted_archives');