Created
March 3, 2020 10:05
-
-
Save sungraiz/2a6a776270029cce83a08ac716436ea6 to your computer and use it in GitHub Desktop.
Agents Sorting
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
| <div class="rh_page__head"> | |
| <div class="rh_page__controls"> | |
| <div class="rh_sort_controls"> | |
| <select name="sort-properties" id="sort-properties" class="rh_select2"> | |
| <option value="default" <?php echo ( !isset($_GET['sortby'])) ? 'selected' : ''; ?>><?php esc_html_e('Default Order', 'framework'); ?></option> | |
| <option value="title" <?php echo ( isset($_GET['sortby']) && 'title' === $_GET['sortby'] ) ? 'selected' : ''; ?>><?php esc_html_e('Agent Name', 'framework'); ?></option> | |
| <option value="total-asc" <?php echo ( isset($_GET['sortby']) && 'total-asc' === $_GET['sortby'] ) ? 'selected' : ''; ?>><?php esc_html_e('Total Properties (ASC)', 'framework'); ?></option> | |
| <option value="pub-asc" <?php echo ( isset($_GET['sortby']) && 'pub-asc' === $_GET['sortby'] ) ? 'selected' : ''; ?>><?php esc_html_e('Published Properties (ASC)', 'framework'); ?></option> | |
| </select> | |
| </div> | |
| </div> | |
| </div> | |
| <?php | |
| if( isset($_GET['sortby']) && ('total-asc' === $_GET['sortby'] || 'pub-asc' === $_GET['sortby'] ) ){ | |
| $agents = array( | |
| 'post_type' => 'agent', | |
| 'numberposts' => -1, | |
| ); | |
| $agent_list = get_posts( $agents ); | |
| if( ! empty($agent_list)){ | |
| $agent_ids = wp_list_pluck($agent_list, 'ID'); | |
| $properties_agents= []; | |
| $agent_sorted = []; | |
| foreach($agent_ids as $agent_id){ | |
| // Prepare query arguments. | |
| $properties_args = array( | |
| 'post_type' => 'property', | |
| 'posts_per_page' => - 1, | |
| 'meta_query' => array( | |
| array( | |
| 'key' => 'REAL_HOMES_agents', | |
| 'value' => $agent_id, | |
| 'compare' => '=', | |
| ), | |
| ), | |
| ); | |
| if ( 'total-asc' === $_GET['sortby']) { | |
| $properties_args['post_status'] = 'any'; | |
| } | |
| if ( 'pub-asc' === $_GET['sortby']) { | |
| $properties_args['post_status'] = 'publish'; | |
| } | |
| $properties = new WP_Query( $properties_args ); | |
| if ( $properties->have_posts() ) { | |
| $properties_agents[$agent_id] = $properties->found_posts; | |
| } | |
| } | |
| if( !empty($properties_agents) && is_array($properties_agents)){ | |
| $properties_agents_sorted = arsort($properties_agents, SORT_NUMERIC); | |
| $agent_sorted = array_keys($properties_agents); | |
| if( !empty($agent_sorted) ){ | |
| $agents_query['post__in'] = $agent_sorted; | |
| $agents_query['order'] = 'ASC'; | |
| $agents_query['orderby'] = 'post__in'; | |
| } | |
| } | |
| } | |
| } | |
| if ( isset($_GET['sortby']) && 'title' === $_GET['sortby']) { | |
| $agents_query['orderby'] = 'title'; | |
| $agents_query['order'] = 'ASC'; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment