admin管理员组

文章数量:1122832

I'm building a website with an archive page where I can filter posts by date (year and month) and by term. I'm using wordpress url params to display filtered posts. The filtered url looks like that : /?taxonomy=category&term=et-aperiam&year=2024&monthnum=5

It works perfectly on the blog page with the native post type post.

I'm trying to do the same thing for a custom post type archive page. But when I try to reach the url /?taxonomy=custom_taxonomy&term=random-term&monthnum=07&year=2024, wordpress rewrite this url to /?taxonomy=custom_taxonomy&term=random-term.

I tried to remove all rewrite rules with 'year' or 'monthnum' on it with

add_filter( 'rewrite_rules_array', 'custom_remove_rewrite_rules' );

function custom_remove_rewrite_rules( $rules ) {
  foreach ( $rules as $rule => $rewrite ) {
    if ( preg_match('/(year=\$matches|monthnum=\$matches)/', $rewrite) ) {
      unset( $rules[$rule] );
    }
  }
  return $rules;
}

but it's not working at all : rewriting is still doing his job even if rules with year on monthnum are removed.

Do you have any hint ? Do you know why this url is not effective on the blog page, but rewriting on a custom post type archive page ?

Thank you !

I'm building a website with an archive page where I can filter posts by date (year and month) and by term. I'm using wordpress url params to display filtered posts. The filtered url looks like that : http://website.com/blog-page/?taxonomy=category&term=et-aperiam&year=2024&monthnum=5

It works perfectly on the blog page with the native post type post.

I'm trying to do the same thing for a custom post type archive page. But when I try to reach the url http://website.com/my_custom_post_type/?taxonomy=custom_taxonomy&term=random-term&monthnum=07&year=2024, wordpress rewrite this url to http://website.com/2024/07/?taxonomy=custom_taxonomy&term=random-term.

I tried to remove all rewrite rules with 'year' or 'monthnum' on it with

add_filter( 'rewrite_rules_array', 'custom_remove_rewrite_rules' );

function custom_remove_rewrite_rules( $rules ) {
  foreach ( $rules as $rule => $rewrite ) {
    if ( preg_match('/(year=\$matches|monthnum=\$matches)/', $rewrite) ) {
      unset( $rules[$rule] );
    }
  }
  return $rules;
}

but it's not working at all : rewriting is still doing his job even if rules with year on monthnum are removed.

Do you have any hint ? Do you know why this url is not effective on the blog page, but rewriting on a custom post type archive page ?

Thank you !

Share Improve this question edited Jul 12, 2024 at 13:12 Pof asked Jul 12, 2024 at 12:00 PofPof 1134 bronze badges 1
  • I found a trick using different params name in the url (cur_year instead of year and cur_monthnum instead of monthnum) and adding a pre_get_post to filter posts by monthnum and year, but I'm still curious about how to remove this date rewriting ! – Pof Commented Jul 12, 2024 at 14:02
Add a comment  | 

1 Answer 1

Reset to default 0

You have just a little mistake in your preg_match, instead of this:

if ( preg_match('/(year=\$matches|monthnum=\$matches)/', $rewrite) ) {

do this:

if ( preg_match('/(year=\$matches|monthnum=\$matches\/)/', $rewrite) ) {

I was looking for a solution to clean a lot of rules, this source is useful: https://wpreset.com/remove-default-wordpress-rewrite-rules-permalinks/

My final code is this, it can be customized allowing more or less rules:

add_filter( 'rewrite_rules_array', function ( $rules ) {
  foreach ( $rules as $rule => $rewrite ) {
    
    // remove most stock rewrite rules 
    if ( preg_match( '/(wp-json|feed|attachment|archives|trackback|comment|author|year|search|category|embed|tag|register|page\/)/', $rule ) ) {
      unset( $rules[$rule] );
    }
    // remove year and month rules
    if ( preg_match('/(year=\$matches|monthnum=\$matches\/)/', $rewrite) ) {
      unset( $rules[$rule] );
    }
    
  }
  
  // uncomment these 2 lines to check the result, open the permalink page
  // echo nl2br( var_export( $rules, true ) );
  // die;
  
  return $rules;
});

本文标签: url rewritingRemove date rewrite rule for custom post type archive page