Unserialize, Serialize again and Update values - mysql

I'm working on a migrated WordPress, site url changed, everything works fine except some serialized data stored by a plugin.
The data is stored in the wp_options table, in the option_value column, for each record that the plugin saved.
So the data is still there, the problem is that when the url changed, it didnt get re-serialized (the count of the string content still thinks it's the old url lenght), therefore the plugin isn't working properly.
So, to find exactly the records that need to be updated, I use
$t1 = $wpdb->prefix . "vslider";
$t2 = $wpdb->prefix . "options";
$records_ineed = $wpdb->get_results("SELECT * FROM '".$t1."', '".$t2."' WHERE '".$t1."'.option_name='".$t2."'.option_name");
That gives me exactly the records that I need to re-serialize (it matches the name of the record created in the plugin table, with the record created in the wp_option table)
What do I do now?!
How do I take only the option_value of each, unserialize it, reserialize it and update the existing value in the db?
Should I save the reserialized values to a new table, and then replace from that table back to wp_options? How?
Otherwise what are other solutions?

There is no need to do that , what you need is to import the value as - is . it is an array and it will work fine as such as long as you do not alter the data in any way.
At any event , when you import migrate a wp site to another URL , there is absolutely no need to read the wp-options tableĀ“s columns one-by-one .
What you need to do it just dump your SQL from the Original ( old-domain ) site , then import into the new-domain , and then run these queries :
/**
To update WordPress options with the new blog location, use the following SQL command:
**/
UPDATE wp_options SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
/**
After that you will need to fix URLs of the WordPress posts and pages, which translated from post slug, and stored in database wp_posts table as guid field. The URL values in this field are stored as abolute URLs instead of relative URLs, so it needs to be changed with the following SQL query:
**/
UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');
/**
If you have linked internally within blog posts or pages with absolute URLs, these links will point to wrong locations after you move the blog location. Use the following SQL commands to fix all internal links to own blog in all WordPress posts and pages:
**/
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');
Just for verification , after running those commands , go back to the options table and verify that your home and site_url functions are both correct as for the new-domain data.
If you do still want , for some obscure reason, insert data directly into the options table manually ( ?? why ) then you should consider using get_post_meta() and update_post_meta() function which will take care of the serialization.

Related

How Do I Bulk Delete All Wordpress Posts in a Specific Category or Before a Specific Date?

I have some categories on a site with over 350,000 active posts some of which have as many as 50,000 posts. The volume makes using a bulk delete plugins not viable because they all timeout and delete nothing. The best feedback I've gotten from the Wordpress support forum is to use WP-CLI but beyond that all they've done is give me a link to the WP-CLI website which contains no example specific to my needs.
I have never used CLI before beyond a failed attempt to run Media Cleaner which resulted in a timeout at 999 seconds. That was months ago so I forget how I tried to do that. Could someone please post a code snippet that could basically be copied and pasted into CLI with the only change I need to make being swapping the category ID or date/ID number of the oldest post I want to keep?
Again, I want to be able to delete all posts from a specific category or before a specific date/ID.
Alternatively, could someone post a SQL statement so that I can do this using MySQL without installing CLI? The SQL would be better because I could apply it to any site without installing let alone learning CLI.
You don't have access to phpmyadmin?
You could avoid all of that just by deleting posts on load:
/**
* REMOVE EVIL POSTS IF AND WHEN LOADED
* add to functions.php
* works on regular posts and simple categories,
* check codex for details if custom post types, custom taxonomies, etc.
**/
add_action( 'template_redirect', 'delete_post_on_load' ) ;
function delete_post_on_load() {
global $post ;
//has term works more consistently if all variables are supplied
if ( has_term( array( 'Category to Go', 'Second Category to Go' ), 'category', $post->ID ) ) {
wp_delete_post( $post->ID, true ) ;
wp_safe_redirect( site_url() ) ; //redirect to home instead of 404 on deletion
exit() ;
}
}

Migrating Wordpress with divi or elementor

I use this commands to migrate in the old day, but with divi or elementor, some crash with migration, anyone use other way ? other idea ? It's due to Json ?
UPDATE wp_options
SET option_value = replace(option_value, 'http://www.old-site.com', 'https://www.new-site.com')
WHERE option_name = 'home'
OR option_name = 'siteurl';
UPDATE wp_posts
SET guid = REPLACE (guid, 'http://www.old-site.com', 'https://www.new-site');
UPDATE wp_posts
SET post_content = REPLACE (post_content, 'https://www.old-site.com', 'https://www.new-site.com');
UPDATE wp_postmeta
SET meta_value = REPLACE (meta_value, 'http://www.old-site.com','https://www.new-site.com');
Thanks
You shouldn't change the guid as that will cause RSS readers to think that all of the posts are now new unread posts.
You might be missing some stuff to replace. Typically when migrating to the new site you do the find and replace on everything in the database (except for the guid). There are some tools that can help you out with this. If you use wp-cli, you can use the wp search-replace command. Otherwise, this is a great tool. Both of these tools will automatically handle doing a proper search and replace inside of any serialized strings.
Also remember to clear out your cache.

Update everything in a database that is like something in mysql

I have a database and i have a home URL that I want to change into another one. The problem is that this home URL is used in a lot of table and in a lot of column.
So is there a MySQL request that do something like :
UPDATE * SET * = REPLACE(*, 'old-url', 'new-url') WHERE * LIKE '%old-url%'
?
Have a good day ! :)
You could use the plugin "Go Live Update URLS". You just enter the old & new URL and select the tables you want to replace the value.
https://nl.wordpress.org/plugins/go-live-update-urls/
Back-up your database before executing the replacement!

Delete duplicate rows, do not preserve one row

I need a query that goes through each entry in a database, checks if a single value is duplicated elsewhere in the database, and if it is - deletes both entries (or all, if more than two).
Problem is the entries are URLs, up to 255 characters, with no way of identifying the row. Some existing answers on Stack Overflow do not work for me due to performance limitations, or they use uniqueid which obviously won't work when dealing with a string.
Long Version:
I have two databases containing URLs (and only URLs). One database has around 3,000 urls and the other around 1,000.
However, a large majority of the 1,000 urls were taken from the 3,000 url database. I need to merge the 1,000 into the 3,000 as new entries only.
For this, I made a third database with combined URLs from both tables, about 4,000 entries. I need to find all duplicate entries in this database and delete them (Both of them, without leaving either).
I have followed the query of a few examples on this site, but whenever I try to delete both entries it ends up deleting all the entries, or giving sql errors.
Alternatively:
I have two databases, each containing the separate database. I need to check each row from one database against the other to find any that aren't duplicates, and then add those to a third database.
Since you were looking for a SQL solution here is one. Lets assume that your table has a single column for simplicity sake. However this will work for any number of fields of course:
CREATE TABLE `allkindsofvalues` (
`value` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The following series of queries will accomplish what you are looking for:
CREATE TABLE allkindsofvalues_temp LIKE allkindsofvalues;
INSERT INTO allkindsofvalues_temp SELECT * FROM allkindsofvalues akv1 WHERE (SELECT COUNT(*) FROM allkindsofvalues akv2 WHERE akv1.value = akv2.value) = 1;
DROP TABLE allkindsofvalues;
RENAME TABLE allkindsofvalues_temp to allkindsofvalues;
The OP wrote:
I've got my own PHP solution which is pretty hacky, but works.
I went with a PHP script to accomplish this, as I'm more familiar with PHP than MySQL.
This generates a simple list of urls that only exist in the target
database, but not both. If you have more than 7,000 entries to parse
this may take awhile, and you will need to copy/paste the results
into a text file or expand the script to store them back into a
database.
I'm just doing it manually to save time.
Note: Uses MeekroDB
<pre>
<?php
require('meekrodb.2.1.class.php');
DB::$user = 'root';
DB::$password = '';
DB::$dbName = 'testdb';
$all = DB::query('SELECT * FROM old_urls LIMIT 7000');
foreach($all as $row) {
$test = DB::query('SELECT url FROM new_urls WHERE url=%s',
$row['url']);
if (!is_array($test)) {
echo $row['url'] . "\n";
}else{
if (count($test) == 0) {
echo $row['url'] . "\n";
}
}
}
?>
</pre>

What's the fastest way to check if a URL already exists in a MySQL table? [duplicate]

This question already has an answer here:
If url already exists in url table in mysql. Break operation in php script
(1 answer)
Closed 9 months ago.
I have a varchar(255) column where I store URL's in a MySQL database. This column has a unique index.
When my crawler encounters a URL, it has to check the database to see if that URL already exists. If it exists, the crawler selects data about that entry. If it does not exist, the crawler adds the url. I currently do this with the following code:
$sql = "SELECT id, junk
FROM files
WHERE url = '$url'";
$results = $this->mysqli->query( $sql );
// the file already exists in the system
if( $results->num_rows > 0 )
{
// store data to variables
}
// the file does not exists yet... add it
else
{
// insert new file
$sql = "INSERT INTO files( url )
VALUES( '$url' )";
$results = $this->mysqli->query( $sql );
}
I realize there are lots of ways to do this. I've read that using a MySQL if/else statement could speed this up. Can someone explain how MySQL would handle that differently, and why that may be faster? Are there other alternatives I should test? My crawlers are doing a lot of checking like this, and speeding up this process could be a significant speed boost for my system.
First of all, URLs are going to get much longer than varchar(256).
Second of all, because they're that long you don't want to do string compares, it gets very slow as the table grows. Instead, create a column with a hash value and compare that.
You should index the hash column, of course.
As for the actual insert, an alternative is to put a unique constraint on the hash. Then do your inserts blindly, allowing SQL to reject the dupes. (But you'll have to put an exception handler into your code, which has its own overhead.)
Considering not using transactions, to insert a new row if an old row does not exist by the WHERE condition, you can use:
"INSERT INTO files( url ) VALUES ( $url ) WHERE NOT EXISTS ( SELECT * FROM files WHERE url = $url );"
I can't think of a "one-line-commond" to select and insert at the same time.
I would do the insert first and check for success(affected_rows), then select. If you check first, and then do the insert, the possibility exists that the url got inserted during that small time window. And, you would need to add more code to handle this situation.