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

How to use get_post_modified_time filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 8, 2023
5 minutes read

get_post_modified_time filter

Filters the localized time a post was last modified.

To use get_post_modified_time filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

Access Premium WordPress Plugins

We at WePlugins, always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_get_post_modified_time_defaults which takes 3 parameters and we registered using add_filter. The first parameter get_post_modified_time is name of the hook, The second parameter modify_get_post_modified_time_defaults is name of the function which need to be called, third parameter is the priority of calling the hook if same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometime, you have to remove a registered hook so you can use remove_filter to remove get_post_modified_time filter.

Parameters

Below are the 3 parameters are required to use this hook.

  • $time : (string|int) Formatted date string or Unix timestamp if $format is ‘U’ or ‘G’.
  • $format : (string) Format to use for retrieving the time the post was modified. Accepts ‘G’, ‘U’, or PHP date format. Default ‘U’.
  • $gmt : (bool) Whether to retrieve the GMT time. Default false.

Example 1: Basic Usage

Below is an example how you can use this hook.

        function weplugins_modify_get_post_modified_time_defaults($time, $format, $gmt) { 
            // Update the $time variable according to your website requirements and return this variable. You can modify the $time variable conditionally too if you want.
            return $time; 
        }
        // add the filter
        add_filter("get_post_modified_time", "weplugins_modify_get_post_modified_time_defaults", 10, 3);
        

Example 2: Removing a Hook Callback

To remove a hook callback, use the example below.

        remove_filter("get_post_modified_time", "weplugins_modify_get_post_modified_time_defaults", 10, 3);
        

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Example 3: Conditional Modification

You can conditionally modify the $time variable based on specific criteria:

        function weplugins_conditional_modify_get_post_modified_time($time, $format, $gmt) {
            if ($format == 'U') {
                // Custom logic for Unix timestamp
                $time = strtotime('now');
            }
            return $time;
        }
        add_filter("get_post_modified_time", "weplugins_conditional_modify_get_post_modified_time", 10, 3);
        

Contact Us

If you’re having any trouble using this hook and need customization, please contact us and we’d be happy to assist you.

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.