Skip to content

Instantly share code, notes, and snippets.

@Inzman
Last active April 5, 2018 08:23
Show Gist options
  • Select an option

  • Save Inzman/fdd709601b7f93a1b2a6576980bc8916 to your computer and use it in GitHub Desktop.

Select an option

Save Inzman/fdd709601b7f93a1b2a6576980bc8916 to your computer and use it in GitHub Desktop.
Fix Wordpress slugs to match their titles
<?php
if(is_user_logged_in()){
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish',
'suppress_filters' => false
);
$pages = get_posts($args);
echo '<table>';
foreach($pages as $page){ //pre($page);
$old_slug = $page->post_name;
$new_slug = slugify($page->post_title);
$warning = ($old_slug != $new_slug) ? 'style="background:red;"': '';
echo '<tr '.$warning.'><td style="padding:10px;">'.$page->post_title.'</td><td style="padding:10px;">'.$old_slug.'</td><td style="padding:10px;">'.$new_slug.'</td></tr>';
if($old_slug != $new_slug){
$my_post = array(
'ID' => $page->ID,
'post_name' => $new_slug
);
wp_update_post($my_post);
}
}
echo '</table>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment