How to make WordPress orderby work with post_excerpt column?

I can use WordPress in-built orderby to sort playlist by any column except post_excerpt

    echo wp_playlist_shortcode( array(
                        'ids' = '7,8,9',
                        'order' = 'DESC',
                        'orderby' = 'post_excerpt', 
    ));

WordPress Codex apparently states 'post_excerpt' is not a valid parameter for the 'orderby' parameter, and nobody nowhere knows how to make orderby work with post_excerpt. How to bypass this limitation?

Topic playlist order excerpt Wordpress

Category Web


I edited the wp_playlist_shortcode function at \wp-includes\media.php WP core file, by adding

    if ($atts['orderby']=='excerpt' || $atts['orderby']=='post_excerpt') {

        function cmp($a, $b) {
            return strcmp($a["caption"], $b["caption"])*(-1);
        }

        usort($tracks, "cmp");
    }

before $data['tracks'] = $tracks; line.

Basically after all tracks are generated into the $tracks array and before this array is passed to the final array $data, I intercepted the code and checked if orderby parameter is set to excerpt or post_excerpt. If true did a usort to sort array descendingly by using caption as criteria. In playlist context, WordPress refers to excerpt as caption. If you need to be sorted ascendingly remove the *(-1) within the cmp function.

Thanks to the @Pat-J for suggesting usort.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.