Last active
October 21, 2026 06:44
-
-
Save jmabbas/389c12ac343a52f84a26b7c87e6e3df0 to your computer and use it in GitHub Desktop.
Electro - Mobile page category block order update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_filter( 'electro_home_mobile_v1_categories_block_args', function( $args ) { | |
| $meta = electro_get_home_mobile_v1_meta(); | |
| if ( ! empty( $meta['hcb']['cat_slugs'] ) ) { | |
| $slug_order = array_map( 'trim', explode( ',', $meta['hcb']['cat_slugs'] ) ); | |
| // Convert slugs to IDs so WordPress fetches only these categories | |
| $term_ids = array(); | |
| foreach ( $slug_order as $slug ) { | |
| $term = get_term_by( 'slug', $slug, 'product_cat' ); | |
| if ( $term && ! is_wp_error( $term ) ) { | |
| $term_ids[] = (int) $term->term_id; | |
| } | |
| } | |
| if ( ! empty( $term_ids ) ) { | |
| $args['category_args']['include'] = $term_ids; | |
| unset( $args['category_args']['slug'] ); | |
| } | |
| // Sort the returned categories in the exact order of the entered slugs | |
| $sort_callback = function( $terms, $taxonomies ) use ( $slug_order, &$sort_callback ) { | |
| if ( is_array( $terms ) && ! empty( $terms ) ) { | |
| usort( $terms, function( $a, $b ) use ( $slug_order ) { | |
| $pos_a = array_search( $a->slug, $slug_order ); | |
| $pos_b = array_search( $b->slug, $slug_order ); | |
| $pos_a = ( false === $pos_a ) ? 9999 : $pos_a; | |
| $pos_b = ( false === $pos_b ) ? 9999 : $pos_b; | |
| return $pos_a <=> $pos_b; | |
| } ); | |
| } | |
| // Clean up filter so it only affects this specific block | |
| remove_filter( 'get_terms', $sort_callback, 10 ); | |
| return $terms; | |
| }; | |
| add_filter( 'get_terms', $sort_callback, 10, 2 ); | |
| } | |
| return $args; | |
| }, 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment