Volete effettuare una ricerca e sostituzione di massa in WordPress?
Se volete trovare e sostituire un testo, un URL o un’immagine specifica sul vostro sito web WordPress, potete farlo facilmente utilizzando un plugin WordPress find-and-replace o una semplice query SQL.
In questo articolo vi mostreremo come trovare e sostituire il testo nel database di WordPress.
Quando è necessario trovare e sostituire nel database di WordPress?
Ci sono alcuni scenari in cui potrebbe essere necessario trovare e sostituire elementi all’interno del database del vostro sito web WordPress.
Ad esempio, supponiamo che abbiate aggiunto un testo o un URL specifico a un certo numero di post. Non sapete a quali post avete aggiunto quel testo, ma sapete che ce ne sono molti.
È possibile cercare manualmente nel sito e modificare ogni singolo post, uno per uno. Ma questo richiederà molto tempo e ha un’alta probabilità di perdere parte del testo che si desidera modificare.
L’utilizzo di un singolo comando per trovare e sostituire automaticamente gli elementi farà la stessa cosa, ma in modo molto più rapido ed efficiente.
Questo metodo ha però un aspetto negativo. Se si commette un errore, non è possibile annullarlo. Una volta sostituito, il testo non esiste più. È necessario digitare con attenzione il testo che si sta cercando e quello con cui si desidera sostituirlo.
Detto questo, vediamo come trovare e sostituire facilmente il testo nel database di WordPress.
Prima di eseguire la ricerca e la sostituzione nel database
Come abbiamo già detto, le modifiche apportate al database non saranno reversibili. È necessario prestare molta attenzione per assicurarsi di non perdere i dati.
Innanzitutto, è necessario creare un backup del database di WordPress. È possibile farlo utilizzando un plugin di backup di WordPress come Duplicator.
In alternativa, è possibile creare un backup del database utilizzando phpMyAdmin.
Dopo aver creato il backup del database di WordPress, si può passare all’esecuzione dei comandi di ricerca e sostituzione.
Esecuzione di Trova e sostituisci con un plugin di WordPress
Se non avete familiarità con il codice e non volete scrivere una query SQL personalizzata, esiste un plugin per WordPress facile da usare, chiamato Better Search Replace.
Per prima cosa, è necessario installare e attivare il plugin Better Search Replace. Per maggiori dettagli, potete consultare la nostra guida su come installare un plugin di WordPress.
Quindi, è necessario visitare la sezione Strumenti ” Ricerca migliore Sostituisci nella dashboard di amministrazione di WordPress.
Da qui, inserire il testo che si desidera modificare nel campo “Cerca”. Successivamente, nel campo “Sostituisci con” si deve scrivere il testo che si desidera sostituire.
È inoltre possibile selezionare tabelle specifiche all’interno del database di WordPress.
Nelle “Impostazioni aggiuntive”, si consiglia di selezionare “Esegui come esecuzione a secco”. In questo modo, il plugin vi permetterà di verificare le modifiche che apporterà prima di applicarle.
Quindi, fare semplicemente clic su “Esegui ricerca/sostituisci”.
Se avete bisogno di ulteriore aiuto, abbiamo una guida dettagliata su come cercare e sostituire in WordPress con il plugin Better Search Replace.
Esecuzione di una query MySQL Find & Replace con phpMyAdmin
È anche possibile utilizzare phpMyAdmin per trovare e sostituire il testo dal database di WordPress.
Per prima cosa, è necessario accedere alla dashboard del proprio account di hosting WordPress. Per questo tutorial, utilizzeremo Bluehost.
Una volta arrivati a questo punto, fate clic sull’opzione “Siti web” dalla colonna di sinistra e poi passate alla scheda “Avanzate”.
Successivamente, scorrere fino alla sezione “PHPMyAdmin” e fare clic sul pulsante “Gestisci” accanto ad essa.
Questo avvierà phpMyAdmin in una nuova finestra.
Qui, dovete cliccare sul nome del vostro database di WordPress e poi su “SQL”.
È quindi necessario inserire la query SQL in questo formato:
update TABLE_NAME set FIELD_NAME =
replace(FIELD_NAME, 'Text to find', 'text to replace with');
Ad esempio, se si volesse cercare del testo nel contenuto di un post di WordPress, si scriverebbe la query in questo modo:
update wp_posts set post_content =
replace(post_content,'Text to find','text to replace with');
Quindi, fare clic sul pulsante “Vai” per continuare.
phpMyAdmin eseguirà la query SQL e, in caso di successo, mostrerà il numero di righe interessate dalla query.
Ora potete visitare il vostro sito WordPress per vedere le modifiche in azione.
Speriamo che questo articolo vi abbia aiutato a capire come trovare e sostituire il testo con un clic nel vostro database WordPress. Vi consigliamo di consultare anche la nostra guida per principianti sulla gestione del database di WordPress con phpMyAdmin e la nostra selezione dei migliori plugin per database di WordPress.
Se questo articolo vi è piaciuto, iscrivetevi al nostro canale YouTube per le esercitazioni video su WordPress. Potete trovarci anche su Twitter e Facebook.
Syed Balkhi says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Chris says
Can do search and replace HTML code? I have snippets of (table td tr) code I would like to replace.
WPBeginner Support says
It would depend on where that code was added from, you should normally be able to edit the page that it is on for modifying the code.
Admin
Shahala says
Hey there! could you please tell me how can I do this find and replace in sql for a word only in specific language?
I tried using language queries but din’t work out!
I really appreciate any help towards this. Thank you
WPBeginner Support says
Unless I hear otherwise, we don’t have a language specific search at the moment sadly.
Admin
Saud Shah says
How can we change 2018 to 2019 in categories and page titles with this plugin or phpmyadmin
WPBeginner Support says
For the post and page titles, you would search the wp_posts table but, for changing categories and their urls you would want to do that manually and create 301 redirects.
Admin
Aminu says
please, how do you replace texts in posts title?
Vishal says
Thanks a lot for this. I was looking for the exact same solution.
Anang says
Thanks a lot. It saves me hours of work.
RCdiy says
I want to replace all instances of “OpenTx” with “OpenTX” but skipping URLs. So basically only what the viewer sees… How do I do this? I’ve installed both Search Replace and Better Search Replace but they also replace in the URLs…
Piter says
I tried plugins “Search $ replace” and “Better Search Replace” but there was no effect. Links in my posts was there. But…
Thanks a lot for code metod, it works fine. It takes only few seconds.
samowal says
Thank you very much, man.
Peter says
You already wrote an article about this last year: https://www.wpbeginner.com/plugins/search-and-replace-plugin-for-wordpress-database/
WPBeginner Support says
Thanks for noticing This is actually a rewrite of an old article that only showed the code method. We decided to add the plugin method and more explanation to it.
Admin
KMurphy says
Is this something that would work in migrating a site – to change the DB urls to a new location?
Kim Hopkins says
I just did this.
Oscar Estepa says
I’m not 100% sure but I’d say the SQL query can’t handle serialised data. If you search & replace your database using the suggested SQL query you’ll break the serialised data and get yourself into lots of troubles down the road like widgets not working, images dissappearing, broken javascript, etc.
It’s well explained in this link http://davidcoveney.com/782/mysql-database-search-replace-with-serialized-php/
So better look for a plugin, script or tool which handles serialised data, like the suggested plugin Better Search Replace or the interconnectit script.
Personally I use search and replace tool integrated in WP-CLI, in my experience it’s the best and fastest tool out there, never had a problem so far, tested in dozens of websites. For less savvy people, I’d recommend a plugin since WP-CLI is a terminal command tool and might not suite everyone.
Damien Carbery says
I like internetconnect/it’s Database Search and Replace Script.
Unlike the phpMyAdmin example in the article, it will search all fields. It will also do a dummy run.