How to remove all untagged posts in wordpress?

Hello. The site has a lot of records with labels and without. You only need to remove entries without labels, it is desirable to keep in the trash if possible. How to implement it? Is there a plugin? Found plugin Bulk Delete, but it deletes only the records with a particular tag.
March 12th 20 at 07:59
1 answer
March 12th 20 at 08:01
Solution
Make a backup!
Here's the code that removes in the basket.
$args = array(
 'post_type' => 'post',
 'posts_per_page' => -1,
 'tax_query' => array(
array(
 'taxonomy' => 'post_tag',
 'operator' => 'NOT EXISTS',
)
)
);
$query = new WP_Query( $args );
foreach($query->query() as $post){
wp_delete_post($post->ID);
}
Not working:( On the local machine open server gives the warning "Missing argument 1" on the line "foreach($query->query() as $post) {"and just hosting "Page is not available, the website can't process this request". - osbaldo55 commented on March 12th 20 at 08:04
Well, so try:
$args = array(
 'post_type' => 'post',
 'posts_per_page' => -1,
 'tax_query' => array(
array(
 'taxonomy' => 'post_tag',
 'operator' => 'NOT EXISTS',
)
)
);
$query = new WP_Query;
$data = $query->query( $args );
foreach($data as $post){
 echo $post->ID . " deleted<br>";
}
die();
- Ellis commented on March 12th 20 at 08:07
Yes, thank you! Just added the line wp_delete_post($post->ID);
and the line 'post_status' => 'publish', array - osbaldo55 commented on March 12th 20 at 08:10

Find more questions by tags WordPress