Remove all posts except bulk URLs list

jootess

Regular Member
Joined
Feb 17, 2022
Messages
240
Reaction score
27
Hello, I want to remove all posts except bulk URLs from MySql.
How can I do it?
 
DELETE FROM posts WHERE url NOT IN ('url1', 'url2', 'url3', ...);
 
DELETE FROM posts WHERE url NOT IN ('url1', 'url2', 'url3', ...);
I can not find url column in the wp-posts table!!!
Where is the url column? Which table?
 
DELETE FROM posts WHERE url NOT IN ('url1', 'url2', 'url3', ...);
I tried this code workes properly but som of our URLs has emoji or non-english chars... This code not work for those URLs. What should I do?
 
I can not find url column in the wp-posts table!!!
Where is the url column? Which table?

There is no such column as far as I know. WordPress configures blog routing the way you set it up in the global settings. So there is no URL column!
You will have to delete based on the blog's title...
 
I tried this code workes properly but som of our URLs has emoji or non-english chars... This code not work for those URLs. What should I do?
You should convert those emojis and NE characters to one that are accepted. Check URL encode/decode tools online
 
DELETE FROM posts WHERE url NOT IN ('url1', 'url2', 'url3', ...);
I want to run this command by adding a second condition that the date of the post is published before September 27, what should I do? It means that all the posts that have the first condition and also the second condition should be deleted
 
Hey,

Not sure if you still need help with this. But I wrote a tad more clear format for you.

1. Getting Post IDs from URL

First we convert your URLs to Post IDs

We use the WordPress url_to_postid function to do this

PHP:
<?php

// 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);
?>

You can save this as a PHP file such as postIDs.php and add it to the root of your WordPress installation. Then visit domain.com/postIDs.php

It should give you an output like below

Code:
Post IDs to delete: 201, 197, 22, 343, 664, 5552

2. Running SQL to delete the posts and associated orphan 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 will only affect date where it's 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.

Hope this helps, let me know if you're stuck or need anymore help.
 
Hey,

Not sure if you still need help with this. But I wrote a tad more clear format for you.

1. Getting Post IDs from URL

First we convert your URLs to Post IDs

We use the WordPress url_to_postid function to do this

PHP:
<?php

// 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);
?>

You can save this as a PHP file such as postIDs.php and add it to the root of your WordPress installation. Then visit domain.com/postIDs.php

It should give you an output like below

Code:
Post IDs to delete: 201, 197, 22, 343, 664, 5552

2. Running SQL to delete the posts and associated orphan 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 will only affect date where it's 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.

Hope this helps, let me know if you're stuck or need anymore help.
Hi, im going to try this again, but i get a Timeout Error when i execute the SQL because i have like 30k posts. I asked my hosting support and they said me that i cant modify the timeout server. Do you know a hosting or a trick to execute it?? Thanks
 
Hi, im going to try this again, but i get a Timeout Error when i execute the SQL because i have like 30k posts. I asked my hosting support and they said me that i cant modify the timeout server. Do you know a hosting or a trick to execute it?? Thanks

You can do it in smaller batches.

Copy a lower number of post IDs to the SQL at a time.

Run them, then repeat.
 
Back
Top