skip to Main Content

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


  1. It is working for me

    UPDATE `wp_posts` SET post_title = REPLACE(post_title, '2024', '2025'),post_name = REPLACE(post_name, '2024', '2025') WHERE post_type = 'post' AND (post_title LIKE '%2024%' OR post_name LIKE '%2024%');
    

    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 referencing wp_posts

    Login or Signup to reply.
  2. If you have not disabled revisions in WordPress you must also include the "revision" post_type in the query:

    ... AND ( post_type = 'post' OR post_type = 'revision' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search