简体   繁体   中英

How to query multiple SQL tasks in php for Wordpress?

I am looking for a way how to carry out multiple sql tasks using Wordpress php script.

So far, I have the following function, but it wont carry out all the tasks atm.

private function rename_products($product) {
    global $wpdb;

    // Replace product names and descriptions
    $sql = "UPDATE wp_posts SET post_title = REPLACE(post_title, 'Velikost Osuška', 'Osuška, Velikost');";
    $sql = "UPDATE wp_posts SET post_title = REPLACE(post_title, 'Velikost Ručník 50x100Velikost Ručník 50 x 100', 'Ručník, Velikost 50x100');";
    $sql = "UPDATE wp_posts SET post_content = REPLACE(post_content, 'Velikost Ručník 50x100Velikost Ručník 50 x 100', 'Ručník, Velikost 50x100');";
    $sql = "UPDATE wp_posts SET post_excerpt = REPLACE(post_excerpt, 'Velikost Ručník 50x100Velikost Ručník 50 x 100', 'Ručník, Velikost 50x100');";

    $wpdb->query($sql);
}

Any ideas?

You can merge all the queries into one and get all the updated done. An update query supports multiple updates using SET statement separated by a comma.

UPDATE wp_posts
SET post_title = REPLACE(post_title, 'Velikost Osuška', 'Osuška, Velikost'),
post_title = REPLACE(post_title, 'Velikost Ručník 50x100Velikost Ručník 50 x 100', 'Ručník, Velikost 50x100'),
post_content = REPLACE(post_content, 'Velikost Ručník 50x100Velikost Ručník 50 x 100', 'Ručník, Velikost 50x100'),
post_excerpt = REPLACE(post_excerpt, 'Velikost Ručník 50x100Velikost Ručník 50 x 100', 'Ručník, Velikost 50x100')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM