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
December 2, 2022
5 minutes read

post_rewrite_rules filter

Filters rewrite rules used for “post” archives.

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

Description

This is a filter hook, which is used for filtering 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

Basic Usage

To run the hook, copy the example below.

    $post_rewrite = apply_filters('weplugins_post_rewrite_rules', $post_rewrite);

    if (!empty($post_rewrite)) {
        // everything has led up to this point...
    }
    

Adding a Hook Callback

The following example is for adding a hook callback.

    // define the weplugins_post_rewrite_rules callback
    function weplugins_filter_post_rewrite_rules($post_rewrite) {
        // make filter magic happen here...
        return $post_rewrite;
    };

    // add the filter
    add_filter('weplugins_post_rewrite_rules', 'weplugins_filter_post_rewrite_rules', 10, 1);
    

Removing a Hook Callback

To remove a hook callback, use the example below.

    // remove the filter
    remove_filter('weplugins_post_rewrite_rules', 'weplugins_filter_post_rewrite_rules', 10, 1);
    

Access Premium WordPress Plugins

Contact Us

If you need customization, Contact Us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

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.