0

I'd like to be able to track changes in the slug / post_name field of the post.

I've tried using wp_save_post_revision(...); if I detect a slug change, but according to this page wordpress only tracks specific fields, post_name not being one of them.

Am I SOL or is there a way to accomplish this?

2

If you're comfortable with PHP, it would be possible to keep track of when the slug is updated. The only way I can think of is a bit complicated:

  1. Make sure revisions are enabled, both for your install and for the post type you're targeting. You'll have to determine how many revisions you want to save, which will be a balance between how many times you think someone might change the post (whether it's a slug change, or content, or anything about the post) and how bloated you allow the database to become with all that extra data.

  2. Add version control for a postmeta field you will use to keep track of the slug with every revision. First install the plugin "WP-Post-Meta-Revisions." Then, in a custom plugin, or a child theme's functions.php file:

add_filter('wp_post_revision_meta_keys', 'wpse_track_slug_postmeta');

function wpse_track_slug_postmeta() {
    // Access the array of postmeta keys that is already versioned
    global $versionedKeys;

    // Add your custom postmeta key to the array
    $versionedKeys[] = 'versionedSlug';

    // Pass the array back to WordPress
    return $versionedKeys;
}

Once this is done, every time you save a revision, the postmeta "versionedSlug" for that specific revision will be saved.

  1. Every time a revision is saved, you need to tell WP to save the slug as that "versionedSlug" postmeta. You can use the wp_insert_post hook:
// Every time WP inserts a post, run this function
add_action('wp_insert_post', 'wpse_save_slug_postmeta');

function wpse_save_slug_postmeta($post_id, $post, $update) {
    // Make sure this is a revision
    if(wp_is_post_revision($post_id)) {
        // Save the slug as postmeta
        // First parameter is the post ID of the revision
        // Second parameter is what you want to call this postmeta key
        // Third parameter is what you're saving - the slug itself
        update_post_meta($post->ID, 'versionedSlug', $post->slug);
    }
}

You will then have to decide how you want to access this data. If you just want to browse a post's revisions, the revision comparison view will show you when the slug changed because it's now explicitly saved as postmeta. You could also create various reports or widgets, depending on your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.