Created
August 10, 2015 18:00
-
-
Save Fitoussi/8eb52f21abf10bdedf25 to your computer and use it in GitHub Desktop.
Posts Locator Order-by Filter tutorial ( part 7 )
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
| function gmw_orderby_filter( $clauses, $gmw ) { | |
| global $wpdb; | |
| //check if order-by value was submitted. If it was we will use its value otherwise we set the default value to 'distance' | |
| $orderby_value = ( isset( $_GET['gmw_orderby'] ) && !empty( $_GET['gmw_orderby'] ) ) ? $_GET['gmw_orderby'] : 'distance'; | |
| //check the value of the order-by and modify the clause based on that | |
| //when order-by distance | |
| if ( $orderby_value == 'distance' ) { | |
| /* | |
| * when we do order-by distance we must check an address was entered. | |
| * we cannot order results by distance when ther is no address to calculate distance to | |
| * and so we will rsults with an error. | |
| * So we check for the address and if address found we will order results by distance. | |
| * Otherwise, we can order results by a different value. In this example i use post_title | |
| */ | |
| if ( isset( $gmw['org_address'] ) && !empty( $gmw['org_address'] ) ) { | |
| //order by distance when address entered | |
| $clauses['orderby'] = 'distance'; | |
| } else { | |
| //if no address order by post title | |
| $clauses['orderby'] = $wpdb->prefix.'posts.post_title'; | |
| } | |
| } elseif ( $orderby_value == 'post_title' ) { | |
| $clauses['orderby'] = $wpdb->prefix.'posts.post_title'; | |
| } elseif ( $orderby_value == 'post_date' ) { | |
| $clauses['orderby'] = $wpdb->prefix.'posts.post_date'; | |
| } elseif ( $orderby_value == 'post_id' ) { | |
| $clauses['orderby'] = $wpdb->prefix.'posts.ID'; | |
| } elseif ( $orderby_value == 'random' ) { | |
| $clauses['orderby'] = 'RAND()'; | |
| } | |
| //return modified clauses | |
| return $clauses; | |
| } | |
| add_filter( 'gmw_pt_location_query_clauses', 'gmw_orderby_filter', 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment