Forum Replies Created
-
AuthorPosts
-
earlygroove
MemberI just saw your response. Changing the filter for the documentation code still doesn’t affect the search results
1234567891011121314151617181920212223242526272829303132333435363738add_action( 'gmw_search_form_before_distance', 'gmw_orderby_dropdown', 20 );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_ajaxfmspt_posts_query_clauses', 'gmw_orderby_filter', 20, 2 );earlygroove
MemberI also tried modifying the search query to see if gmw_pt_search_query_args works as expected, but whether I do the query args like
123456789101112131415161718192021function gmw_modify_query_by_date( $meta_args, $gmw ) {if ( 1 == $gmw['ID'] || 2 == $gmw['ID'] || 3 == $gmw['ID'] ) {$tod = new DateTime('today');$today = $tod->format('Y-m-d');$args = array ('post_type' => 'ajde_events','orderby' => 'meta_value_num','meta_key' => 'new_dateA','meta_query' => array(array('key' => 'new_dateA','value' => $today,'compare' => '=',),),);return $args;}add_filter( 'gmw_pt_search_query_args', 'gmw_modify_query_by_date', 10, 2 );or if I use the format
1$args['post_type'] = 'ajde_events';this does not modify the search query.
Nor does modifying the meta_query
123456789101112131415161718function events_today( $args, $gmw ) {if ( $gmw['ID'] == 2) {//we only need to modify the meta_query argument$args['meta_query'] = array(array('key' => 'new_dateA','value' => $today,'compare' => '='));}return $args;}add_filter( 'gmw_pt_search_query_args','events_today', 10, 2 );To be clear, my end goal is not to modify the search query without triggering it within a select box –just thought I’d try modifying the query straight out since modifying the clauses was not working.
earlygroove
MemberHi. Modified query clauses don’t appear to be working at all.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354function 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'] : '';?><select class="gmw-orderby-dropdown" name="gmw_orderby"><option value="distance" selected="selected">Distance</option><option value="post_title" <?php if ( $selected == 'post_title' ) echo 'selected="selected"'; ?>>Alphabetically</option><option value="post_date" <?php if ( $selected == 'post_date' ) echo 'selected="selected"'; ?>>Post Date</option><option value="rating" <?php if ( $selected == 'today' ) echo 'selected="selected"'; ?>>Today</option></select><?php}add_action( 'gmw_search_form_before_distance', 'gmw_orderby_dropdown', 20 );function gmw_orderby_filter( $clauses, $gmw ) {global $wpdb;$tod = new DateTime('today');$today = $tod->format('Y-m-d');//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 == 'today' ) {//apply_filters( 'gmw_pt_meta_query', $today_meta );$clauses['join'] .= " LEFT JOIN {$wpdb->prefix}postmeta AS pmeta ON({$wpdb->prefix}posts.ID = pmeta.post_id AND pmeta.meta_key = 'new_dateA')";$clauses['orderby'] = "pmeta.meta_value, new_dateA";}//return modified clausesreturn $clauses;}add_filter( 'gmw_pt_location_query_clauses', 'gmw_orderby_filter', 20, 2 );I would like to return only the posts that have the meta_value ‘new_dateA’ == $today , but even the orderby meta_value is not working.
Even this code from the docs does NOT modify the query:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152function 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'] : '';?><select class="gmw-orderby-dropdown" name="gmw_orderby"><option value="distance" selected="selected">Order By</option><option value="distance" <?php if ( $selected == 'distance' ) echo 'selected="selected"'; ?>>Distance</option><option value="post_title" <?php if ( $selected == 'post_title' ) echo 'selected="selected"'; ?>>Alphabetically</option><option value="post_date" <?php if ( $selected == 'post_date' ) echo 'selected="selected"'; ?>>Post Date</option><option value="post_id" <?php if ( $selected == 'post_id' ) echo 'selected="selected"'; ?>>Post ID</option><option value="random" <?php if ( $selected == 'random' ) echo 'selected="selected"'; ?>>Random</option></select><?php}add_action( 'gmw_search_form_before_distance', 'gmw_orderby_dropdown', 20 );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 );Do these not work for ajax forms, or what is the issue?
I’m really having trouble finding code examples in the docs or forums that actually work, from which to build on. It’s impossible to tell which are outdated or only apply to form results that load on a new page.
earlygroove
MemberThis reply has been marked as private.earlygroove
MemberHi again, I see the code here https://docsdep.geomywp.com/forums-site/topic/taxonomy-and-custom-field-filters-not-working/ being the closest to what I’m trying to do , but I have multiple queries.
For example, I want to have search filters for relative dates, such as ‘today’, ‘tomorrow’, and ‘this weekend’ — I have these custom queries coded and working on their own,
{I had the code here but it encodes basic characters (?) so I’ve now removed it}
but I’m not sure how to attach each query to its own search filter option. Can you please point the right direction, or send a quote for creating this customization yourself?
-
AuthorPosts