Search code examples
phpmysqlsqlwordpress

Wordpress Mysql query to replace empty alt img with post_title


I have several empty img alt on my wordpress site (about 3k). After considering different solutions, looks like the best approach is to do a replace on the database. I would like to replace empty alt in post_content (alt="") with post_title (alt="This is the post title").

Something like that:

update wp_posts
set post_content = REPLACE(post_content,'alt=""', 'alt=" + post_title + "') 
where post_status = 'publish';

But I don't get how to insert the post_title value in the query. Thank you for your help


Solution

  • use a query like this:

    UPDATE wp_posts
    set post_content = 
      REPLACE(post_content,'alt=""', CONCAT('alt="', post_title, '"')) 
    WHERE post_status = 'publish';
    

    and for a specific lenght use SUBSTRING()

    UPDATE wp_posts
    set post_content = 
      REPLACE(post_content,'alt=""', CONCAT('alt="', SUBSTRING(post_title,1,20), '"')) 
    WHERE post_status = 'publish';