Skip to content
Advertisement

How to get posts order by menu_order in WordPress using $wpdb->get_results()?

I have the following function where I build out my own parent-child navigation.

The issue I’m facing is I have not been able to order the children by menu_order ( so order by the Order attribute that can be set with each page when re-ordered ). Any tips on how to do so would be greatly appreciated.

function wph_get_page_tree( $page_id ){
    global $wpdb;
    $tree_data = [];
    $tree_data[$page_id] = ['title' => get_post($page_id)->post_title, 'ID' => $page_id];


    // get 1 level childs
    $level_1_childs = [];
    $all_childs = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM  {$wpdb->posts} WHERE post_parent = %d && post_type= 'page' && post_status='publish' ", $page_id ) );
    foreach( $all_childs as $single_2_level ){

        $level_1_childs[$single_2_level->ID] = ['title' => $single_2_level->post_title, 'ID' => $single_2_level->ID];
        //$level_1_childs[$single_2_level->ID] = $single_2_level->post_title;
        $all_sub_childs = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM  {$wpdb->posts} WHERE post_parent = %d && post_type= 'page' && post_status='publish' ", $single_2_level->ID ) );

        $level_2_childs = [];
        foreach( $all_sub_childs as $single_3_level ){
            $level_2_childs[$single_3_level->ID] = ['title' => $single_3_level->post_title, 'ID' => $single_3_level->ID];
        }
        //var_dump( $level_1_childs );
        $level_1_childs[$single_2_level->ID]['childs'] = $level_2_childs;
        //var_dump( $level_1_childs );
    }
    $tree_data[$page_id]['childs'] = $level_1_childs;

    //var_dump( $tree_data );
    return $tree_data;
}

Advertisement

Answer

You need to pass ORDER BY {$wpdb->posts}.menu_order ASC to your query. check the below code.

function wph_get_page_tree( $page_id ){
    global $wpdb;
    $tree_data = [];
    $tree_data[$page_id] = ['title' => get_post($page_id)->post_title, 'ID' => $page_id];


    // get 1 level childs
    $level_1_childs = [];
    $all_childs = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM  {$wpdb->posts} WHERE post_parent = %d && post_type= 'page' && post_status='publish' ORDER BY {$wpdb->posts}.menu_order ASC ", $page_id ) );
    foreach( $all_childs as $single_2_level ){

        $level_1_childs[$single_2_level->ID] = ['title' => $single_2_level->post_title, 'ID' => $single_2_level->ID];
        //$level_1_childs[$single_2_level->ID] = $single_2_level->post_title;
        $all_sub_childs = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM  {$wpdb->posts} WHERE post_parent = %d && post_type= 'page' && post_status='publish' ORDER BY {$wpdb->posts}.menu_order ASC ", $single_2_level->ID ) );

        $level_2_childs = [];
        foreach( $all_sub_childs as $single_3_level ){
            $level_2_childs[$single_3_level->ID] = ['title' => $single_3_level->post_title, 'ID' => $single_3_level->ID];
        }
        //var_dump( $level_1_childs );
        $level_1_childs[$single_2_level->ID]['childs'] = $level_2_childs;
        //var_dump( $level_1_childs );
    }
    $tree_data[$page_id]['childs'] = $level_1_childs;

    //var_dump( $tree_data );
    return $tree_data;
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement