Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Tips And Tricks Delete Bulk Draft And Trash WordPress Posts Includes All Tags, Comments, Meta Fields And Terms Associated

Delete Bulk Draft And Trash WordPress Posts Includes All Tags, Comments, Meta Fields And Terms Associated

In WordPress, if you need to delete a particular published post and all its related contents (tags, comments, meta data, …) you have to move it to the Trash folder first. After that, you have to go to Trash folder to delete that post. WordPress allows you to do that with just few clicks by choosing multiple posts in one time to Move to Trash and Delete Permanently It’s pretty cool, isn’t it?

However, if you’re going to delete thousands of dump/out of date posts, it could be a problem. You have to do the same process again and again in few days even a whole week.

Today, I’ll show How I completely delete a Bulk of posts (12000) in 5 minutes. The process could be faster or slower depend on your server performance. You is required to have a little bit knowledge on PHP Programming and have FTP log in detail to upload your file.

Let create a new file (ex: _delete_posts.php) in your WordPress web root folder and replace its content with the source code below, it will permanently delete 12000 posts in drafts folder or you can change post_status to trash to delete all posts in Trash folder.

<?php
	set_time_limit(600);
	require_once('wp-load.php');
 
	wp();
 
	$sql_get_posts = "select wp_posts.ID FROM wp_posts  WHERE wp_posts.post_type = 'post' AND (wp_posts.post_status = 'draft')  ORDER BY wp_posts.post_modified DESC LIMIT 0, 12000";
 
	$posts = $wpdb->get_results( $sql_get_posts );
 
	foreach ($posts as $post)
	{
		wp_delete_post( $post->ID, true );
		echo "Deleted post id: " . $post->ID . "<br>";
	}
 
	echo "OK";
	exit();
?>

<?php set_time_limit(600); require_once('wp-load.php'); wp(); $sql_get_posts = "select wp_posts.ID FROM wp_posts WHERE wp_posts.post_type = 'post' AND (wp_posts.post_status = 'draft') ORDER BY wp_posts.post_modified DESC LIMIT 0, 12000"; $posts = $wpdb->get_results( $sql_get_posts ); foreach ($posts as $post) { wp_delete_post( $post->ID, true ); echo "Deleted post id: " . $post->ID . "<br>"; } echo "OK"; exit(); ?>

Note:

  • 1. Replace the ‘wp_’ with your table prefix in wp-config.php file
  • 2. Modify the SQL Command (sql_get_posts) in order to filter post IDs you want to delete
  • 3. Understand which SQL commands will access to your database by Monitoring all MySQL activities
  • 4. Just run the script on Production server after you Double check on development environment.
  • 5. Backup your database
  • 6. Run Optimize Table command for affected tables after you delete bulk of posts.

Post comment if you have any question, I’m here to share and help 🙂

Good luck 🙂

Jul 8, 2011Hoan Huynh
Replace String In MySQLGet Image Width Height With JQuery And JavaScript
You Might Also Like:
  • Delete WordPress Revision Posts, Its Relationships And Meta Data
  • WordPress Delete Unused Post Tags By SQL Command
  • Get WordPress Most Popular Posts By Total Comments
  • WordPress Get Recent Posts And Recent Comments
  • How To Get WordPress Tags By Categories
  • WordPress Get All Custom Fields
  • PHP Delete File Function
  • Auto Delete Old IIS Logs, FTP Logs, SMTP Logs In Windows
  • Add More Extra Informations Or Fields To WordPress User Profile
  • Disable Pingbacks and Trackbacks In WordPress
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

11 years ago Tips And Tricksget_results, How To, require_once, set_time_limit, Wordpress, wp_delete_post520
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,455 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,836 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,652 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,986 views
C# Read Json From URL And Parse/Deserialize Json
11,719 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development