- This topic has 14 replies, 2 voices, and was last updated 6 years, 12 months ago by
squints.
-
AuthorPosts
-
June 29, 2015 at 7:07 am #39270
squints
MemberI read the tutorial on how to create an order-by dropdown filter here: http://docs.tempform.wpengine.com/posts-locator-order-by-filter/
But it doesn’t seem to be working. I have placed the following code in my functions.php file:
123456789101112131415161718192021222324252627282930313233343536373839404142434445function gmw_orderby_dropdown( $gmw ) {//getting the chosen value so we could pre-select the selected value after page load$selected = ( isset( $_GET['gmw_orderby'] ) && !empty( $_GET['gmw_orderby'] ) ) ? $_GET['gmw_orderby'] : '';do_action( 'gmw_search_form_before_distance', $gmw );}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 distanceif ( $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 clausesreturn $clauses;}add_filter( 'gmw_pt_location_query_clauses', 'gmw_orderby_filter', 20, 2 );I thought using the action hook in the first function would automatically display the filter above the distance dropdown in a search form but it did not. I also tried using
gmw_orderby_dropdown( $gmw );
in the search form template but this also does not display anything.Do I have an error in my code somewhere, or am I missing something?
Thanks!
July 1, 2015 at 10:25 am #39404squints
MemberBump
July 2, 2015 at 10:43 am #39474Eyal Fitoussi
MemberHello Squints,
Looks like the code in this tutorial is “broken”. I will need to update this page and I will do so after the release of Geo my WP 2.6.1. I will update you once I update the tutorial so you can check the script again.July 3, 2015 at 10:58 am #39517squints
MemberOkay thanks.
I got certain aspects of the dropdown filter to work by just building a simple dropdown selector in the search form using the <select> tags then using the provided php function in functions.php. But the code for the actual dropdown doesn’t work.
But I also really need to know how to add a custom option to the sort dropdown. I am attempting to add the ability to sort by a meta value but cannot get it to work.
Here is the code I have tried:
123elseif ( $orderby_value == 'highest_rating' ) {$meta_key = 'rating_avg';$clauses['orderby'] = $wpdb->postmeta.'meta_value DESC';and:
12345678910elseif ( $orderby_value == 'highest_rating' ) {$meta_key = 'rating_avg';$clauses['orderby'] = $wpdb->get_col( $wpdb->prepare("SELECT meta_keyFROM $wpdb->postmeta meta_keyORDER BY meta_value",$meta_key) );But these return no results.
So how do I add a sort option using a meta value in the functions.php code you have provided?
Thanks.
July 9, 2015 at 9:29 am #40000squints
Member…
July 10, 2015 at 12:27 pm #40090Eyal Fitoussi
MemberHi,
I apologize but I am currently out of the country and I don’t have a way to look into the code or test it at the moment. So I really have no way to help you with this script above.However, iT does look like you are in the right direction.
July 12, 2015 at 9:59 am #40179squints
MemberWhen will you be able to look at/test the code?
Thanks.
July 13, 2015 at 11:49 am #40239Eyal Fitoussi
MemberBY the end of this month.
August 1, 2015 at 7:03 am #41543squints
MemberI was hoping you would have a solution by the end of the month as you claimed. Are you going to provide a solution for this?
August 2, 2015 at 7:55 am #41576Eyal Fitoussi
MemberI will take a look it within a few days. I just arrived back in the country and I am now taking care of few urgent bugs found in the latest version of GEO my WP.
August 2, 2015 at 11:25 am #41591squints
MemberOkay I will be eagerly waiting.
thx
August 4, 2015 at 10:23 am #41735squints
MemberI have realized that the reason the sort method is not working is something to do with the GMW url parameters. If I am displaying a loop of posts outside of GMW and append
?r_sortby=highest_rated
to the URL then the posts are properly sorted by the meta value. But since GMW uses the URL param?gmw_orderby=
I cannot get the sort to work. I have tried passing?r_sortby=highest_rated
in GMW and it results in an error page and when using?gmw_orderby=highest_rated
it is not recognized.So how can I pass the URL param
?r_sortby=highest_rated
for this one sort option instead of the default?gmw_orderby=
?August 9, 2015 at 7:36 am #42094squints
MemberCan you please update the tutorial or at least just answer how to add a filter option based on meta data to the existing code? It’s been a month and a half already…
I have been putting off purchasing 2 of your Add-ons that I need because I can’t even get support for a simple question.
August 10, 2015 at 8:20 am #42152Eyal Fitoussi
MemberHi Squints,
I updated the code in the tutorial. However, it looks like you will need to go a different direction if you want to order by meta_key. You will need to modify the WP_Query instead of the location query clauses.Below is the WP_Query that GEO my WP creates:
1234567891011//query args$this->form['query_args'] = apply_filters( 'gmw_pt_search_query_args', array('post_type' => $post_types,'post_status' => array( 'publish' ),'tax_query' => apply_filters( 'gmw_pt_tax_query', $tax_args, $this->form ),'posts_per_page' => $this->form['get_per_page'],'paged' => $this->form['paged'],'meta_query' => apply_filters( 'gmw_pt_meta_query', $meta_args, $this->form ),'ignore_sticky_posts' => 1,'orderby' => 'distance'), $this->form );You will need to use the filter ‘gmw_pt_search_query_args’ to modify the WP_Query args based on the order that you need.
This should give you a start point. Other than that I will need to rewrite the tutorial based on this example and that I will be able to do only once I am done with all the updates and bugs fixes.
August 11, 2015 at 7:57 am #42220squints
MemberOkay thank you for the reply. Are we getting close to a solution for the default value issue addressed in this topic: https://tempform.wpengine.com/support/topic/how-to-set-a-default-value-in-search-form/
-
AuthorPosts
You must be logged in to reply to this topic.