The add_trashed_suffix_to_trashed_posts WordPress PHP filter allows you to control whether a ‘__trashed’ suffix is added to trashed posts that have the same name as an updated post.
Usage
add_filter('add_trashed_suffix_to_trashed_posts', 'your_custom_function', 10, 3); function your_custom_function($add_trashed_suffix, $post_name, $post_id) { // your custom code here return $add_trashed_suffix; }
Parameters
$add_trashed_suffix
: bool – Whether to attempt to add the suffix.$post_name
: string – The name of the post being updated.$post_id
: int – Post ID.
More information
See WordPress Developer Resources: add_trashed_suffix_to_trashed_posts
Examples
Prevent adding suffix to trashed posts
Prevent adding the ‘__trashed’ suffix to all trashed posts.
add_filter('add_trashed_suffix_to_trashed_posts', '__return_false');
Add suffix to specific post type
Add the ‘__trashed’ suffix only to trashed posts with a specific post type.
add_filter('add_trashed_suffix_to_trashed_posts', 'add_suffix_to_specific_post_type', 10, 3); function add_suffix_to_specific_post_type($add_trashed_suffix, $post_name, $post_id) { $post_type = 'your_post_type'; return (get_post_type($post_id) == $post_type) ? true : $add_trashed_suffix; }
Add suffix based on post author
Add the ‘__trashed’ suffix only to trashed posts with a specific author.
add_filter('add_trashed_suffix_to_trashed_posts', 'add_suffix_based_on_author', 10, 3); function add_suffix_based_on_author($add_trashed_suffix, $post_name, $post_id) { $author_id = 123; return (get_post_field('post_author', $post_id) == $author_id) ? true : $add_trashed_suffix; }
Add custom suffix
Add a custom suffix instead of the default ‘__trashed’ suffix.
add_filter('wp_unique_post_slug', 'add_custom_suffix', 10, 6); function add_custom_suffix($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug) { global $wpdb; if ($post_status == 'trash') { $suffix = '__custom_suffix'; $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug . $suffix, $post_ID)); if (!$post_name_check) { $slug .= $suffix; } } return $slug; }