| | 134 | * Performs column sorting for taxonomies registered with 'show_column_sortable'. |
| | 135 | * @since 4.0.0 |
| | 136 | * @param array $clauses The list of clauses for the query. |
| | 137 | * @param WP_Query $wp_query The WP_Query instance (passed by reference). |
| | 138 | * |
| | 139 | * @return string Modified clauses |
| | 140 | */ |
| | 141 | function peform_sortable_taxonomy_columns_query( $clauses, $wp_query ) { |
| | 142 | global $wpdb; |
| | 143 | |
| | 144 | if ( ! isset( $wp_query->query['orderby'] ) ) { |
| | 145 | return $clauses; |
| | 146 | } |
| | 147 | |
| | 148 | $taxonomies = get_sortable_taxonomies( $wp_query->query['post_type'] ); |
| | 149 | |
| | 150 | if ( empty( $taxonomies ) || ! is_array( $taxonomies ) ) { |
| | 151 | return $clauses; |
| | 152 | } |
| | 153 | |
| | 154 | $sortable_taxonomies = array(); |
| | 155 | foreach ( $taxonomies as $taxonomy ) { |
| | 156 | if ( 'category' == $taxonomy ) |
| | 157 | $column_key = 'categories'; |
| | 158 | elseif ( 'post_tag' == $taxonomy ) |
| | 159 | $column_key = 'tags'; |
| | 160 | else |
| | 161 | $column_key = 'taxonomy-' . $taxonomy; |
| | 162 | |
| | 163 | $sortable_taxonomies[ $taxonomy ] = $column_key; |
| | 164 | } |
| | 165 | |
| | 166 | $key = array_search( $wp_query->query['orderby'], $sortable_taxonomies, true ); |
| | 167 | |
| | 168 | if ( false === $key ) { |
| | 169 | return $clauses; |
| | 170 | } |
| | 171 | |
| | 172 | $clauses['join'] .= " |
| | 173 | LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id |
| | 174 | LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id) |
| | 175 | LEFT OUTER JOIN {$wpdb->terms} USING (term_id) |
| | 176 | "; |
| | 177 | $clauses['where'] = $wpdb->prepare( " AND taxonomy = '%s'", $key ); |
| | 178 | $clauses['groupby'] = "object_id"; |
| | 179 | $clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) "; |
| | 180 | $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC'; |
| | 181 | |
| | 182 | return $clauses; |
| | 183 | } |
| | 184 | add_filter( 'posts_clauses', 'peform_sortable_taxonomy_columns_query', 10, 2 ); |
| | 185 | |
| | 186 | /** |
| 3934 | | } |
| 3935 | | No newline at end of file |
| | 3990 | } |
| | 3991 | |
| | 3992 | /** |
| | 3993 | * Get a list of all taxonomies registered as sortable. |
| | 3994 | * Passes through the "manage_taxonomies_for_{$post_type}_sortable_columns" filter. |
| | 3995 | * |
| | 3996 | * @since 4.0.0 |
| | 3997 | * @param string $post_type Post type to retrieve Taxonomies |
| | 3998 | * |
| | 3999 | * @return array Array of sortable taxonomies |
| | 4000 | */ |
| | 4001 | function get_sortable_taxonomies( $post_type ) { |
| | 4002 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| | 4003 | $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true, 'show_column_sortable' => true ), 'and', 'name' ); |
| | 4004 | |
| | 4005 | /** |
| | 4006 | * Filter the sortable taxonomy columns in the Posts list table. |
| | 4007 | * |
| | 4008 | * The dynamic portion of the hook name, $post_type, refers to the post |
| | 4009 | * type slug. |
| | 4010 | * |
| | 4011 | * @since 3.5.0 |
| | 4012 | * |
| | 4013 | * @param array $taxonomies Array of taxonomies to enable sortable columns for. |
| | 4014 | * @param string $post_type The post type. |
| | 4015 | */ |
| | 4016 | $taxonomies = apply_filters( "manage_taxonomies_for_{$post_type}_sortable_columns", $taxonomies ); |
| | 4017 | $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' ); |
| | 4018 | |
| | 4019 | return $taxonomies; |
| | 4020 | } |