Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use post_rewrite_rules filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 6, 2022
5 minutes read
Share Via:

post_rewrite_rules filter

Filters rewrite rules used for “post” archives.

apply_filters( 'post_rewrite_rules', string[] $post_rewrite )

Description

This is filter hook , which is used for filters the rewrite rules used for “post” archives.

With post_rewrite_rules, you can easily change permalinks not only to the post but also to its comments, attachments, etc. Original permalinks you can keep or replace with new ones.

Parameters

  • $post_rewrite : (string[]) Array of rewrite rules for posts, keyed by their regex pattern.

Live Example

apply_filters( 'post_rewrite_rules', string[] $post_rewrite )

To run the hook, copy the example below.

$post_rewrite = apply_filters( 'post_rewrite_rules', $post_rewrite ); 
                         
if ( !empty( $post_rewrite ) ) { 
                         
   // everything has led up to this point... 
                         
} 
  
  

The following example is for adding a hook callback.

// define the post_rewrite_rules callback 
function filter_post_rewrite_rules( $post_rewrite ) { 
    // make filter magic happen here... 
    return $post_rewrite; 
}; 
         
// add the filter 
add_filter( 'post_rewrite_rules', 'filter_post_rewrite_rules', 10, 1 ); 

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_rewrite_rules', 'filter_post_rewrite_rules', 10, 1 ); 

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.