9

By default WordPress pushes out a whole array of pages I don’t want or need - archive, author, blog, monthly, recent posts, categories, comments, attachment, taxenomies, search, search results, and probably a few others I’ve missed.

Most of the time I’m building regular broshure websites with no need for anything except a few fairly static pages. How do I get rid of all the other pages? Is there a plugin that will do this? Do I have to set up a dozen redirects in the .htaccess? I’ve tried searching, but all I find is how to hide parts of a page, or customise the sitemap to hide from searches. But I don't want those pages at all, so even entering the direct URL shouldn't work.

5 Answers 5

7

You could redirect anything that's not a page or admin to home via the parse_query action:

function wpa_parse_query( $query ){
    if( ! is_admin() && ! $query->is_page() ) {
        wp_redirect( home_url() );
        exit;
    }
}
add_action( 'parse_query', 'wpa_parse_query' );

If it's not an admin screen or a query for a page, it'll redirect. You can see all of the types of pages this will remove under the Conditional Tags page in Codex.

4

Joost de Valk's WordPress SEO plugin is capable of disabling most, if not all, archives you mention:

enter image description here

2

You can user another small script, without adding any plugin. There is a post here and the code to add in the index.php of your theme is this:

if(is_archive()) {
    // force 404
    $wp_query->set_404();
    status_header( 404 );
    nocache_headers();
    include("404.php");
    die;
}

Hope you find it useful.

1

For anyone wondering, I ended up using .htaccess 301 redirects.

# Redirect useless pages
Options +FollowSymlinks
RewriteEngine on
RedirectMatch 301 ^/portfolio/.*$ /gallery/
RedirectMatch 301 ^/author/.*$ /
RedirectMatch 301 ^/category/.*$ /
RedirectMatch 301 ^/tag/.*$ /
RedirectMatch 301 ^/20.*$ /

The blogpost archive ^/20.*$ isn't ideal, but it will have to do for now. Also don't know what other pages I might've missed.

2
  • Note that .*$ is useless and slows your regex down. So just ^/portfolio/ is enough to catch an page that starts with portfolio. I also wonder whether you should not return a 404 instead. Commented Mar 7, 2018 at 17:33
  • As for the last one, you could at least verify that there are 4 digits as in: ^/20[0-9][0-9]. So now you must have 2000 to 2099. Commented Mar 7, 2018 at 17:35
1

It can also be achieved using the template_redirect hook.

You can check for each type of archives pages, or "disable" them altogether.

  • Return a 404error or redirect to another page, I prefer the 404 approach-

In functions.php :

/* Disable archives pages */
add_action('template_redirect', 'my_disable_archives_function');

function my_disable_archives_function()
{
  /* Conditional checks examples:
      is_category() 
      is_tag()
      is_date()
      is_author()
      is_tax()
      is_search() ... */

    // Return a 404 for all archive types, except the my_custom_post_type archive.
    $post_types = array('my_custom_post_type');

  if ( (is_archive() && !is_post_type_archive( $post_types )) )
  {
      global $wp_query;
      $wp_query->set_404();
      status_header(404);
  }
}

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.