Sort array by custom string value and number

        // sort function

        function wpd_last_name_sort( $a, $b ) {

         $a_last = end(explode(' ', $a->post_title));

         $b_last = end(explode(' ', $b->post_title));    

         return strcasecmp( $a_last, $b_last );

        }

        function wpd_menu_order_sort( $a, $b ) {

            $a_last = $a->menu_order;

         $b_last = $b->menu_order;

         return $a_last > $b_last;

        }   

        

        // sort posts

        $menu_order_tags = [

            'cardiothoracic-surgery',

            'oncology',

            'paediatric-surgery',

        ];

        if ( $specialists->query && in_array($specialists->query['tag'], $menu_order_tags)) {

            usort( $specialists->posts, 'wpd_menu_order_sort' );

            echo '<!-- ' . $specialists->query['tag'] . ' is wpd_menu_order_sort -->';

        } else {

            usort( $specialists->posts, 'wpd_last_name_sort' );

            echo '<!-- ' . $specialists->query['tag'] . ' is wpd_last_name_sort -->';

        }

Leave a Reply

Your email address will not be published. Required fields are marked *