Created
February 6, 2025 14:14
-
-
Save chikaibeneme/31ac4ef6cc260deab162f99d1f1500eb to your computer and use it in GitHub Desktop.
attendee-info-sortable-column.php
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
| /** | |
| * Adjust the query to sort by "Attendee Information" on the tickets attendees page. | |
| * | |
| * Assumes attendee information is stored as post meta under 'attendee_info'. | |
| * | |
| * @param WP_Query $query The current WP_Query instance. | |
| */ | |
| function optimized_sort_attendee_column( $query ) { | |
| if ( | |
| is_admin() && | |
| $query->is_main_query() && | |
| ! empty( $_GET['page'] ) && | |
| 'tickets-attendees' === $_GET['page'] | |
| ) { | |
| $orderby = ! empty( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : ''; | |
| if ( 'primary_info' === $orderby ) { | |
| $query->set( 'meta_key', 'attendee_info' ); | |
| $query->set( 'orderby', 'meta_value' ); | |
| } | |
| } | |
| } | |
| add_action( 'pre_get_posts', 'optimized_sort_attendee_column' ); | |
| /** | |
| * Inject JavaScript to convert the "Attendee Information" header into a clickable link | |
| * for sorting, with the sorting indicators placed immediately after the header text. | |
| */ | |
| function optimized_modify_tickets_attendees_header_js() { | |
| if ( is_admin() && ! empty( $_GET['page'] ) && 'tickets-attendees' === $_GET['page'] ) : ?> | |
| <script type="text/javascript"> | |
| jQuery(function($) { | |
| var $header = $('th#primary_info'); | |
| if ( $header.length && !$header.find('a').length ) { | |
| try { | |
| var url = new URL(window.location.href); | |
| } catch(e) { | |
| return; | |
| } | |
| var currentOrder = url.searchParams.get('order') || 'asc'; | |
| var newOrder = ( currentOrder === 'asc' && url.searchParams.get('orderby') === 'primary_info' ) ? 'desc' : 'asc'; | |
| url.searchParams.set('orderby', 'primary_info'); | |
| url.searchParams.set('order', newOrder); | |
| var newUrl = url.toString(); | |
| // Build the HTML as a single line with no extra whitespace | |
| var headerHtml = '<a href="'+newUrl+'">Attendee Information' + | |
| '<span class="sorting-indicators"><span class="sorting-indicator asc" aria-hidden="true"></span><span class="sorting-indicator desc" aria-hidden="true"></span></span>' + | |
| '<span class="screen-reader-text">Sort ascending</span></a>'; | |
| $header.html(headerHtml); | |
| } | |
| }); | |
| </script> | |
| <?php endif; | |
| } | |
| add_action( 'admin_footer', 'optimized_modify_tickets_attendees_header_js' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment