0

it seems that I cannot update multiple rows to change the post_type of posts having a specific tag.

I follow different method, even if it seems the good one is this one :

global $wpdb;
$result = $wpdb->query("UPDATE $wpdb->posts SET $wpdb->posts.post_type = 'editorial_post' WHERE $wpdb->posts.post_type = post AND $wpdb->postmeta.meta_key = 'tag' 
AND $wpdb->postmeta.meta_value = 'editorial'");
var_dump($result);

Var_dump() always returns a null result. Can someone tell me where I'm wrong ? Thank you very much.

  • 1
    From your code, you aren't storing the return value of query to $result – sakibmoon Aug 19 '14 at 16:16
  • Sorry it was a typo, but the results give boolean false – Neovea Aug 20 '14 at 7:03
2

$wpdb->query() returns number of rows affected by the query. It's returning NULL indicates that there is an error in your query.

The problem with your query is that you are updating $wpdb->posts table, but the condition depends on $wpdb->postmeta table. You have to join these tables for the query to work.

Another problem is that you have to place the post inside quote

$wpdb->posts.post_type = 'post'

Try the following query

global $wpdb;
$result = $wpdb->query("UPDATE $wpdb->posts
                        INNER JOIN $wpdb->postmeta
                            ON $wpdb->posts.ID = $wpdb->postmeta.post_id
                        SET $wpdb->posts.post_type = 'editorial_post'
                        WHERE $wpdb->posts.post_type = 'post'
                            AND $wpdb->postmeta.meta_key = 'tag'
                            AND $wpdb->postmeta.meta_value = 'editorial'");
var_dump($result);
|improve this answer|||||
  • Sorry if I didn't answer earlier. I'll give it a try soon. Thanks – Neovea Aug 28 '14 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.