Remove duplicate nodes (dedupe) based on title.
Tagged:  •  

Here is a function that will remove all duplicate nodes (based on title) for a certain content type. It keeps the most recent one.

<?php
function dedupe($type) {
 
$previous = array();
 
$result = db_query("SELECT nid, title FROM {node}
    WHERE title IN
      (SELECT title FROM {node}
        WHERE type = '%s'
        GROUP BY title HAVING count(*) > 1)
    ORDER BY title, created DESC"
, $type);
  while (
$row = db_fetch_array($result)) {
    if (
$row['title'] == $previous['title']) {
     
node_delete($previous['nid']);
    }
   
$previous = $row;
  }
}
?>
Login or register to tag items

Change title

Hi, I am looking for a slightly similar solution for my website but not only do I want to remove the duplicates, I also want to change the title. Now my titles are all caps and I want only the first letter to be a capital. It should take the title string and change it by using a regexp?

We can change the title

We can change the title using CSS, but if you insist to use PHP try this
ucfirst(strtolower(t(your template title variable));

Easy Code

Seems to be easy code. Thanks.

Preserve Category terms

This assumed only 1 term per node but it could be adapted and necessary. Thought I'd provide it because yours helped me out.

<?php
function dedupe ($counter) {
 
$first_row = array();
 
$result = db_query("
    SELECT n.nid, n.title, tn.tid
    FROM {node} n
    LEFT JOIN {term_node} tn ON tn.nid = n.nid
    WHERE title IN
      (SELECT title FROM {node}
        GROUP BY title HAVING count(*) > 1)
    ORDER BY title, created DESC, n.nid ASC"
);
  while (
$row = db_fetch_array($result)) {
    if (
$row['title'] == $first_row['title']) {
     
db_query("INSERT INTO {term_node}
                VALUES (%d, %d, %d)"
, $first_row['nid'], $first_row['nid'], $row['tid']);
     
node_delete($row['nid']);
      print
'Added record for term: ' . $row['tid'] . ' on node: ' . $first_row['nid'] . ' and deleted node: '. $row['nid'];
    } else {
     
$first_row = $row;
      if (
$i++ > $counter) { return;};
    }
  }
}
?>

remove node

hello good article

please help. which placed the code to run with Cronjobs. What needs to be changed if I want to use

regard

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options