How to disable date archives?

admin2025-01-07  6

Is there a way to disable date archives in WordPress?

For example, I don't want to have date archives (example/2021/06/11, for example) available at all. Is there a way to turn these archives off?

I found the following code snippet on Google, but it doesn't appear to alter anything.


add_action( 'template_redirect', 'remove_date_archives' );

function remove_date_archives() {
    // Check if we are on date archive page and return 404
    if ( is_date() ) {
        global $wp_query;
        $wp_query->set_404();
    }
}
?>

Any tips or links are appreciated. Thanks in advance.

Is there a way to disable date archives in WordPress?

For example, I don't want to have date archives (example.com/2021/06/11, for example) available at all. Is there a way to turn these archives off?

I found the following code snippet on Google, but it doesn't appear to alter anything.


add_action( 'template_redirect', 'remove_date_archives' );

function remove_date_archives() {
    // Check if we are on date archive page and return 404
    if ( is_date() ) {
        global $wp_query;
        $wp_query->set_404();
    }
}
?>

Any tips or links are appreciated. Thanks in advance.

Share Improve this question edited Nov 9, 2021 at 16:07 Howdy_McGee 20.8k24 gold badges91 silver badges175 bronze badges asked Nov 9, 2021 at 10:27 Aaron BentleyAaron Bentley 11 bronze badge 4
  • That snippet works for me. What theme are you using? There may be other code that overrides the snippet. Have you tried searching for other instances of 'template_redirect'? – Kevin Marsden Commented Nov 9, 2021 at 18:11
  • 1 Also, what is your permalink structure? – Kevin Marsden Commented Nov 9, 2021 at 18:18
  • Thanks for confirming the snippet is ok - the site theme is Hello-Elementor so I'm now thinking it's related to Elementors templating system? The permalink structure is using /%postname% – Aaron Bentley Commented Nov 11, 2021 at 9:39
  • I tested Hello-Elementor and the Elementor plugin with their default settings, and the snippet still works. Where are you saving the snippet? You can also try using the Query Monitor plugin to see if your custom function is being called by the template_redirect hook. – Kevin Marsden Commented Nov 11, 2021 at 18:09
Add a comment  | 

1 Answer 1

Reset to default 0

Do you happen to use the Yoast SEO plugin? There is an option in the plugin under Search > Archives where you can switch off the Date archives. (and also Author archives if you want to).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264458a969.html

最新回复(0)