Make WordPress Core


Ignore:
Timestamp:
10/05/2017 12:36:43 AM (6 years ago)
Author:
kadamwhite
Message:

REST API: Support ordering response collection by listed slugs.

Adds an "include_slug" orderby value for REST API collections to permit returning a collection filtered by slugs in the same order in which those slugs are specified.
Previously, the order of slugs provided with the ?slug query parameter had no effect on the order of the returned records.

Props wonderboymusic, ocean90, boonebgorges.
Fixes #40826.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r41377 r41760  
    8888     * @since 4.6.0 Introduced 'term_taxonomy_id' parameter.
    8989     * @since 4.7.0 Introduced 'object_ids' parameter.
     90     * @since 4.9.0 Added 'slug__in' support for 'orderby'.
    9091     *
    9192     * @param string|array $query {
     
    99100     *                                                'slug', 'term_group', 'term_id', 'id', 'description', 'parent'),
    100101     *                                                'count' for term taxonomy count, 'include' to match the
    101      *                                                'order' of the $include param, 'meta_value', 'meta_value_num',
     102     *                                                'order' of the $include param, 'slug__in' to match the
     103     *                                                'order' of the $slug param, 'meta_value', 'meta_value_num',
    102104     *                                                the value of `$meta_key`, the array keys of `$meta_query`, or
    103105     *                                                'none' to omit the ORDER BY clause. Defaults to 'name'.
     
    842844            $include = implode( ',', wp_parse_id_list( $this->query_vars['include'] ) );
    843845            $orderby = "FIELD( t.term_id, $include )";
     846        } elseif ( 'slug__in' == $_orderby && ! empty( $this->query_vars['slug'] ) && is_array( $this->query_vars['slug'] ) ) {
     847            $slugs = implode( "', '", array_map( 'sanitize_title_for_query', $this->query_vars['slug__in'] ) );
     848            $orderby = "FIELD( t.slug, '" . $slugs . "')";
    844849        } elseif ( 'none' == $_orderby ) {
    845850            $orderby = '';
Note: See TracChangeset for help on using the changeset viewer.