0

Basically I have around 4000 pages on my wordpress site and I have identified 1320 are duplicates.

I know I can go select each and every one manually and then bulk move them to the trash etc but does anyone know of a more time efficient way to do this?

Perhaps a MYSQL query to delete all the duplicates pages?

An example of the urls is as follows:

(Original)

/page/

(Duplicate)

/page-2/ and sometimes even /page-3/

There must be a way to mass delete all pages with the added -2 or -3

Thanks for any and all help.

Some added information

I created pages depending on locations and I've just realised that I have pages such as

Berwick upon Tweed

and

Berwick-Upon-Tweed

I'd Ideally like to delete the pages with the - in the title as well

  • 1
    Are you sure that the pages with "-2" and "-3", etc., are all duplicate pages and not just duplicate titles? – s_ha_dum Jun 4 '15 at 14:04
  • yes they are all duplicate pages. page-example and page-example-2 both exist unfortunately – Dansghc Jun 4 '15 at 14:06
1

As you assure me that the post title is an accurate indicator of the "duplicate" status of the post, a little bit of SQL and wp_delete_post() will do it:

$sql = "SELECT ID
  FROM {$wpdb->posts} 
  WHERE post_title REGEXP '^.*-[[:digit:]]*$'
  AND post_type = 'page'";
$del = $wpdb->get_col($sql);
foreach ($del as $d) {
  wp_delete_post($d);
}

As written, your duplicates will end up in the "trash", unless that is disabled. You can use...

wp_delete_post($d,true);

... to force deletion.

Based on a comment below:

I created pages depending on locations and I've just realised that I have pages such as Berwick upon Tweed and Berwick-Upon-Tweed I'd Ideally like to delete the pages with the - in the title...

I can't help but think that that is a phenomenally dangerous thing to do, but the SQL is simple:

$sql = "SELECT ID
  FROM {$wpdb->posts} 
  WHERE post_title LIKE '%-%'
  AND post_type = 'page'";
  • It said there was an error with that code however I managed to come up with this... DELETE a.* FROM wp_posts AS a INNER JOIN ( SELECT post_title, MIN( id ) AS min_id FROM wp_posts WHERE post_type = 'page' AND post_status = 'publish' GROUP BY post_title HAVING COUNT( * ) > 1 ) AS b ON b.post_title = a.post_title AND b.min_id <> a.id AND a.post_type = 'page' AND a.post_status = 'publish' This managed to get rid of around 550 of the duplicates but for some reason there are still quite a lot of duplicate pages left – Dansghc Jun 4 '15 at 14:59
  • To be more specific I've narrowed down the problem. I created pages depending on locations and I've just realised that I have pages such as Berwick upon Tweed and Berwick-Upon-Tweed I'd Ideally like to delete the pages with the - in the title... – Dansghc Jun 4 '15 at 15:03
  • The code runs on my server. I tested it. – s_ha_dum Jun 4 '15 at 15:07
  • Sorry there might be some finger trouble from my end however I copy and paste your code into the SQL part of phpMyAdmin and it comes up saying MySQL said: Documentation #1064 - 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 '$sql = "SELECT ID FROM {$wpdb->posts} WHERE post_title REGEXP '^.*-[[:dig' at line 1 – Dansghc Jun 4 '15 at 15:18
  • You are running a MySQL server that doesn't like my REGEXP syntax. The LIKE in the edit should work, but it does do something different. – s_ha_dum Jun 4 '15 at 15:26

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.