Skip to content

Commit

Permalink
Sort filter choices
Browse files Browse the repository at this point in the history
  • Loading branch information
Viljami Hirvonen committed Oct 31, 2022
1 parent 9397818 commit c0a3d0f
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions models/archive-artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ public function filters() {
return [];
}

$categories = $this->sort_terms( $categories );

$base_url = get_post_type_archive_link( Artist::SLUG );
$categories = array_map( function ( $item ) use ( $base_url ) {
return [
Expand Down Expand Up @@ -233,15 +235,7 @@ public function results() {
}
}

$results = array_map( function ( $term ) {
$term->menu_order = get_field( 'menu_order', $term );

return $term;
}, $results );

uasort( $results, fn( $a, $b ) => (int) $a->menu_order > (int) $b->menu_order );

return $results;
return $this->sort_terms( $results );
}

/**
Expand Down Expand Up @@ -295,4 +289,23 @@ public static function format_posts( array $posts ) : array {
return $item;
}, $posts );
}

/**
* Sort terms by menu_order.
*
* @param array $terms Array of WP_Terms.
*
* @return array
*/
protected function sort_terms( $terms ) {
$terms = array_map( function ( $term ) {
$term->menu_order = \get_field( 'menu_order', $term );

return $term;
}, $terms );

uasort( $terms, fn( $a, $b ) => (int) $a->menu_order > (int) $b->menu_order );

return $terms;
}
}

0 comments on commit c0a3d0f

Please sign in to comment.