Reply To: Reply order with menu_order has gaps
Yes, of course. Here is my script. I thought about setting menu_order for spam posts to “0” before the loop.
<?php
require_once('wp-load.php');
function repair_menu_order_gaps()
{
global $wpdb;
$posts = $wpdb->get_results("
SELECT ID FROM $wpdb->posts
WHERE post_type = 'topic' AND post_status = 'publish'
ORDER BY ID ASC
");
$topic_counter = 1;
$unterschiede = 0;
foreach ($posts as $post) {
$poid = $post->ID;
$replies = $wpdb->get_results("
SELECT menu_order, ID FROM $wpdb->posts
WHERE post_type = 'reply' AND post_status = 'publish' AND post_parent=$post->ID ORDER BY menu_order ASC");
$order=0;
$counter = 0;
$ids = array();
$menu_orders = array();
foreach ($replies as $reply) {
$counter++;
$order=$reply->menu_order;
$ids[] = $reply->ID;
$menu_orders[] = $reply->menu_order;
}
if($counter!=$order) {
echo "Abweichung gefunden in Post $poid: Counter $counter , Order = $order </br/>";
$unterschiede++;
$counter_rep = 0;
foreach ($ids as $id) {
$order=$menu_orders[$counter_rep];
$counter_rep++;
echo "Neue Nummer für $id: $counter_rep (Alt: $order)<br/>";
// Set new menu_order
$wpdb->update(
$wpdb->posts,
array('menu_order' => $counter_rep),
array('ID' => $id)
);
}
}
$topic_counter++;
}