Search code examples
mysqlwordpressmariadbpolylang

Search and replace query with multilanguage site(polylang)


I'm trying to update an old link for a specific language in a very big site that is based on wordpress + polylang, I'm using this query

MYSQL

update `wp_posts`
set `post_content` = replace(`post_content`, 'https://old-link', 'https://new-link/')
WHERE `ID` IN (
    SELECT `ID`
    FROM `wp_posts`
    WHERE
    `ID` IN (
    SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(`description`, '"en";i:', -1), ';',1) FROM `wp_term_taxonomy` WHERE `taxonomy` = 'post_translations' )
)

this is always returning to me the error

ERROR

**#1064 - syntax error into SQL query near '://old-link/', 'https://new-link/'))' on line 6**

If I run only the subquery:

MYSQL

SELECT `ID`
FROM `wp_posts`
WHERE
`ID` IN (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(`description`, '"en";i:', -1), ';',1) FROM `wp_term_taxonomy` WHERE `taxonomy` = 'post_translations' )

it will return the right IDs for en language. Where is the error?


Solution

  • you have to be patient.. substantially the subquery returns a table not a string useful to the IN operator, so even if conceptually it is right we have to think differently. Using INNER JOIN I treat the result of the subquery as a useful table for the JOIN itself, so this is the solution for search and replace values in a db based on a wordpress polylang multilanguage site:

    MYSQL:

    UPDATE `wp_posts` AS a
    INNER JOIN (
        SELECT ID
        FROM `wp_posts`
        WHERE
        ID IN (
        SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(`description`, '"en";i:', -1), ';',1) FROM `wp_term_taxonomy` WHERE `taxonomy` = 'post_translations' )
    ) AS b
    ON a.ID = b.ID
    SET a.post_content = replace(a.post_content, 'href="https://old-link/"', 'href="https://new-link/"');