Skip to main content
Add http header status 404. Previous solution returned 'page not found' in wordpress, but http header response was still '200 OK'.
Source Link

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);
  }
}

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();
  }
}

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);
  }
}
Source Link
FFrewin
  • 248
  • 1
  • 13

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();
  }
}