I try to replace 2023 to 2024 in WordPress post_title and post_name(slug), as below:
UPDATE `wp_posts`
SET post_title = REPLACE(post_title, '(2023)', '(2024)'), post_name = REPLACE(post_name, '-2023', '-2024')
WHERE (post_title LIKE '%(2023)%') and (post_type = 'post');
However, after executing the query, although the data in the table are changed, in WP Dashboard, the title and slug of the post are not. Why?
I have tried to clear the cache but do not work.
2
Answers
It is working for me
This query should replace all occurrences of ‘2023’ with ‘2024’ in both the post_title and post_name columns for posts with the post type ‘post’. The % in the LIKE condition allows for matching ‘2023’ anywhere in the title or slug.
I recommend utilizing the global $wpdb object with
$wpdb->posts
instead of directly referencingwp_posts
If you have not disabled revisions in WordPress you must also include the "revision" post_type in the query: