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.

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

1 Answer 1

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);
1
  • Sorry if I didn't answer earlier. I'll give it a try soon. Thanks
    – Neovea
    Commented Aug 28, 2014 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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