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

How to use pre_get_lastpostmodified filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
September 11, 2022
5 minutes read

Ever wondered how to tweak the return value of `get_lastpostmodified()` before the query runs in WordPress? Let me introduce you to the `pre_get_lastpostmodified` filter! It’s super handy and can be a game-changer for your WordPress site.

To use the `pre_get_lastpostmodified` filter, you need to register it using `add_filter`. You can either pop this code into your theme’s `functions.php` file or, better yet, create a custom WordPress plugin. We at WePlugins always recommend creating a custom plugin to ensure that your tweaks remain intact even if you update your WordPress theme.

Below, I’ll walk you through some live examples of how to use this filter.

Access Premium WordPress Plugins

Example 1: Basic Usage

In this example, we’ll define a function `weplugins_modify_pre_get_lastpostmodified_defaults` which takes three parameters. We’ll then register this function using `add_filter`.

    function weplugins_modify_pre_get_lastpostmodified_defaults($lastpostmodified, $timezone, $post_type) { 
        // Update the $lastpostmodified variable according to your website requirements and return this variable.
        return $lastpostmodified; 
    }
    // add the filter
    add_filter("pre_get_lastpostmodified", "weplugins_modify_pre_get_lastpostmodified_defaults", 10, 3);
    

Example 2: Conditional Modification

What if you want to modify the `$lastpostmodified` variable conditionally? No worries! Here’s how you can do it:

    function weplugins_modify_pre_get_lastpostmodified_conditional($lastpostmodified, $timezone, $post_type) { 
        if ($post_type === 'custom_post_type') {
            // Do your custom modification for specific post type
            $lastpostmodified = '2023-01-01 00:00:00';
        }
        return $lastpostmodified; 
    }
    // add the filter
    add_filter("pre_get_lastpostmodified", "weplugins_modify_pre_get_lastpostmodified_conditional", 10, 3);
    

Example 3: Removing the Hook

Sometimes, you might want to remove a registered hook. Here’s how you can use `remove_filter` to do that:

    // remove the filter
    remove_filter("pre_get_lastpostmodified", "weplugins_modify_pre_get_lastpostmodified_defaults", 10, 3);
    

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

Parameters

Below are the 3 parameters required to use this hook:

  • $lastpostmodified: (string|false) The most recent time that a post was modified, in ‘Y-m-d H:i:s’ format, or false. Returning anything other than false will short-circuit the function.
  • $timezone: (string) Location to use for getting the post modified date. See `get_lastpostdate()` for accepted `$timezone` values.
  • $post_type: (string) The post type to check.

If you’re having any trouble using this hook or need some customization, feel free to Contact Us. We’re here to help!

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.