Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply order with menu_order has gaps


  • bastizim
    Participant

    @bastizim

    Hello,

    we have a website with bbpress and Buddypress. In some topics there are gaps in the counter menu_order, so that the pages are calculated incorrectly. For example, a topic with 80 replies has menu_order up to >90, so the reply links link to a page that doesn’t exist.

    Is there a way to fix the menu_order? And why are there such gaps?

Viewing 8 replies - 1 through 8 (of 8 total)

  • Robin W
    Moderator

    @robin-w

    Try dashboard,tools,forums,repair forums and run one at a time


    bastizim
    Participant

    @bastizim

    Thanks for your reply, Robin. I have used the repair functions and the gaps are still there.

    I have installed a local test system and a script, that iterates through the replies and fix the menu_order in wp_posts of every reply with a simple counter.

    It works at the test system, but I’m not sure, if there could be any side effects.


    Robin W
    Moderator

    @robin-w

    I’m away this week, but see no reason why that should not work


    bastizim
    Participant

    @bastizim

    Thank you, I will try that.


    bastizim
    Participant

    @bastizim

    I’ve just seen some replies that have the post_status spam and are responsible for gaps in some topics. The spam-marked replies are part of the menu_order.

    For example:

    ID post_status menu_order
    1 publish 1
    2 spam 2
    3 publish 3
    4 spam 4

    and so on…
    Any idea about that?


    Robin W
    Moderator

    @robin-w

    Yes that would be a good explanation , let me test


    Robin W
    Moderator

    @robin-w

    A copy of your script would be useful


    bastizim
    Participant

    @bastizim

    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++;
        }
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.
Skip to toolbar