- Apr 23, 2012
- 2,964
- 5,938
Hey,
A lot of you are probably building bulk sites or adding on programmatic posts. More often than not you'd want to delete a handful of posts, especially the non-performing ones.
Maybe you got an export from GA4 or GSC and you want to keep the top ones and delete everything else. So here's a code that will help you bulk delete everything.
We'll do it in 2 steps
1. Getting Post IDs from the URL of your WordPress post. PostIDs are unique numbers WordPress assigns to every post on your site
2. Use MySQL to delete these posts and any postmeta associated with them.
1. Getting Post IDs from URL
First we convert your URLs to Post IDs
We use the WordPress
Create a PHP file such as postIDs.php with the below code and add it to the root of your WordPress installation. Then visit domain.com/postIDs.php
Remember to replace the URLs with your own post URLs.
Once you visit this file in your browser, it should give you an output like below
These are the Unique Post IDs we will use to delete our posts.
2. Running SQL to delete the posts and associated postmeta
Then you can run an SQL command such as
The last line is optional. If added, the code will only affect posts where the date is greater than whatever you set it to.
//
P.S - Do take a backup of your database before messing with anything.
P.P.S - Try the SQL and the script with a smaller number of posts first to verify correct post IDs are retrieved and the deletion occurs.
How to get non-performing pages?
1. Export all your top performing pages from GSC or Google Analytics
2. Export all your URLs using something like - https://wordpress.org/plugins/export-all-urls/
Compare 1 against 2 using Excel or Google Sheets to remove all duplicates.
Now you're left with dead/non-performing pages.
Hope this helps, let me know if you're stuck or need anymore help
A lot of you are probably building bulk sites or adding on programmatic posts. More often than not you'd want to delete a handful of posts, especially the non-performing ones.
Maybe you got an export from GA4 or GSC and you want to keep the top ones and delete everything else. So here's a code that will help you bulk delete everything.
We'll do it in 2 steps
1. Getting Post IDs from the URL of your WordPress post. PostIDs are unique numbers WordPress assigns to every post on your site
2. Use MySQL to delete these posts and any postmeta associated with them.
1. Getting Post IDs from URL
First we convert your URLs to Post IDs
We use the WordPress
url_to_postid function to do thisCreate a PHP file such as postIDs.php with the below code and add it to the root of your WordPress installation. Then visit domain.com/postIDs.php
Remember to replace the URLs with your own post URLs.
PHP:
<?php
// I love BlogPro SO SO MUCH! (Say this thrice)
// Include WordPress core files.
define('ABSPATH', dirname(__FILE__) . '/');
include(ABSPATH . 'wp-load.php');
// Define an array of post URLs you want to delete.
$post_urls = array(
'https://domain.com/post1-to-delete/',
'https://domain.com/post2-to-delete'
// Add more URLs as needed.
);
// Initialize an array to store the post IDs.
$post_ids_to_delete = array();
foreach ($post_urls as $url) {
// Get the post ID from the URL.
$post_id = url_to_postid($url);
if ($post_id !== 0) {
// Post ID is valid; add it to the list of IDs to delete.
$post_ids_to_delete[] = $post_id;
}
}
// Output the list of post IDs to delete.
echo 'Post IDs to delete: ' . implode(', ', $post_ids_to_delete);
?>
Once you visit this file in your browser, it should give you an output like below
Code:
Post IDs to delete: 201, 197, 22, 343, 664, 5552
These are the Unique Post IDs we will use to delete our posts.
2. Running SQL to delete the posts and associated postmeta
Then you can run an SQL command such as
SQL:
DELETE posts, postmeta
FROM wp_posts AS posts
LEFT JOIN wp_postmeta AS postmeta ON posts.ID = postmeta.post_id
WHERE posts.ID IN (1, 2, 3, ... /* List of Post IDs */)
AND posts.post_type = 'post'
AND posts.post_date > '2023-09-27 00:00:00';
The last line is optional. If added, the code will only affect posts where the date is greater than whatever you set it to.
//
P.S - Do take a backup of your database before messing with anything.
P.P.S - Try the SQL and the script with a smaller number of posts first to verify correct post IDs are retrieved and the deletion occurs.
How to get non-performing pages?
1. Export all your top performing pages from GSC or Google Analytics
2. Export all your URLs using something like - https://wordpress.org/plugins/export-all-urls/
Compare 1 against 2 using Excel or Google Sheets to remove all duplicates.
Now you're left with dead/non-performing pages.
Hope this helps, let me know if you're stuck or need anymore help

