0

When trashing a post, also trash related comments so here is what i want, what‘s’ the sql Command

I want to move some post to trash according their ID, what is the sql command,I can restore these post and their comments using sql command

2

I would discourage using raw SQL for moving a post to a trash or back, especially since there's a build in function for it:

wp_trash_post( $post_id  ); 

More about the wp_trash_post() function.

The opposite is:

wp_untrash_post( $post_id  );

wp_untrash_post() also untrashes trashed comments.

In WP there are functions for many things - for instance you can trash/untrash only comments, leaving the post alone - in that case use: wp_untrash_post_comments() and wp_trash_post_comments().

| improve this answer | |
  • when i run wp_trash_post($87); it show wrong message in sql command “mysql> wp_trash_post($87); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'wp_trash_post($87)' at line 1” – Furukawa Tomo Jun 26 '17 at 15:30
  • This is a command that you need to run from within the wordpress files having administrator rights, I didn't know you want to run this scripts in the mysql terminal and that is a different story. – Picard Jun 26 '17 at 17:29
  • if want to run this scripts in the mysql terminal ,how? – Furukawa Tomo Jul 11 '17 at 2:29
  • But why would you like to do it in the raw SQL? If you analyze the WP source there are many conditions and dependencies to take care of when un/trashing comments/posts in some cases this could be more than a one simple SQL command. If you'd like to do it from the command line but not exactly from MySQL you can check the WP CLI system - command line interface for WP - for instance here is how to untrash comments – Picard Jul 11 '17 at 6:51
0

Sql Command for trashed a post with comments for that post:

$post_status = 'trash';
$post_array = '1, 2, 3'; //post_ids comma separated

$wpdb->query($wpdb->prepare(                                    
             "UPDATE                          
             `wp_posts`,                                  
             `wp_comments`                                          
              SET                    
              wp_posts.post_status= %s,   
              wp_comments.comment_approved= %s                    
              WHERE                                         
              wp_posts.ID = wp_comments.comment_post_ID AND  
              wp_posts.ID IN ( $post_array )",           
              $post_status,
              $post_status                                 
));
| improve this answer | |

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.