Customize geodirectory advanced search with custom range

If we want to select something tricky in geo directory advance search than it is not available in default like i was want to add duration days for course from 1 to 100 but in front end it will we some range for select with three option

1- Less than 1 week
2-1 week to 1 month
3-1 month or more

So i made some custom code for it .

In the geo directory there are lots of filters for customise search 

1- geodir_search_filter_searched_params

2- geodir_search_posts_where

3 – geodir_search_filter_field_html_output_select


/**
 * Duration days custom output for select .
 *
 * @return $html
 */
function gd_snippet_25052021_search_filter_field_html_output_select( $html, $field, $post_type ) {
	$field_name = 'mt_duration_days';
	if ( ! empty( $html ) && ! empty( $field->htmlvar_name ) && $field->htmlvar_name == $field_name ) {
		$html  = '<div class="geodir-filter-cat  gd-type-single mb-3 gd-field-' . $field_name . '"><label class="text-muted">' . __( $field->frontend_title, 'geodirectory' ) . '</label>';
		$html .= gd_snippet_25052021_search_filter_field_html_output( $html, $field, $post_type, $field_name, false );
		$html .= '</div>';
	}
	return $html;
}
add_filter( 'geodir_search_filter_field_html_output_select', 'gd_snippet_25052021_search_filter_field_html_output_select', 20, 3 );
/**
 * Duration days custom output .
 *
 * @return $html
 */
function gd_snippet_25052021_search_filter_field_html_output( $html, $field, $post_type, $field_name, $main ) {
	$value = isset( $_REQUEST[ 's' . $field_name ] ) ? sanitize_text_field( $_REQUEST[ 's' . $field_name ] ) : '';
	$html  = '<select name="s' . $field_name . '" class="cat_select s_mt_duration_days custom-select form-control"><option value="">' . __( 'All duration', 'geodirectory' ) . '</option>';
	for ( $k = 1; $k <= 3; $k++ ) {
		if ( $k === 1 ) {
			$l     = 7;
			$html .= '<option value="' . $l . '-Less" ' . selected( $l . '-Less than 1 week', $value, false ) . '>Less than 1 week</option>'; // X+
		}
		if ( $k === 2 ) {
			$l      = 7;
			$m      = 30;
			$value1 = $l . '-More-Less' . $m;
			$html  .= '<option value="' . $value1 . '"' . selected( $value1, $value, false ) . '>1 week to 1 month</option>'; // X+
		}
		if ( $k === 3 ) {
			$l     = 31;
			$html .= '<option value="' . $l . '-More" ' . selected( $l . '-More', $value, false ) . '>1 month or more</option>'; // X+
		}
	}
	$html .= '</select>';
	return $html;
}
/**
 * Searched params duration .
 *
 * @return $html
 */

function search_filter_searched_params_duration( $params = array(), $post_type, $fields = array() ) {
	if ( ! empty( $_REQUEST['smt_duration_days'] ) ) {
		$field_title = '';
		$field_name = 'mt_duration_days';
		if ( ! empty( $fields ) ) {
			foreach( $fields as $key => $field ) {
				if ( $field->htmlvar_name == 'mt_duration_days' ) {
					for ( $k = 1; $k <= 31; $k++ ) {
						if ( $_REQUEST[ 's' . $field_name ] === $k . '-More' ) {
							$field_title = __( 'Duration days more than one month', 'geodirectory' );
							break;
						} elseif ( strpos( $_REQUEST[ 's' . $field_name ], '-More-Less' ) !== false ) {
							$field_title = __( 'Duration days 7 to 30', 'geodirectory' );
							break;
						} elseif ( strpos( $_REQUEST[ 's' . $field_name ], '7-Less' ) !== false ) {
							$field_title = __( 'Duration days less than a week', 'geodirectory' );
							break;
						} else {
							$field_title = '';
							
						}
					}
				}
			}
		}
		if ( $field_title ) {
			$params[]     = '<label class="gd-adv-search-label badge badge-info c-pointer gd-adv-search-range gd-adv-search-' . $field_name . '" data-name="s' . $field_name . '"><i class="fas fa-times" aria-hidden="true"></i> ' . $field_title . '</label>';
		}
	}

	return $params;
}
add_filter( 'geodir_search_filter_searched_params', 'search_filter_searched_params_duration', 20, 3 );
/**
 * Add where query for duration days .
 *
 * @return $where sql for duration
 */
function gd_snippet_25052021_search_posts_where( $where, $query = array() ) {
	global $geodir_post_type;
	$field_name = 'mt_duration_days';
	if ( ! empty( $where ) && isset( $_REQUEST[ 's' . $field_name ] ) && $_REQUEST[ 's' . $field_name ] !== '' && GeoDir_Query::is_gd_main_query( $query ) ) {
		$table = geodir_db_cpt_table( $geodir_post_type );
		for ( $k = 1; $k <= 31; $k++ ) {
			if ( $_REQUEST[ 's' . $field_name ] === $k . '-More' ) {
				$where = str_replace( "AND {$table}.{$field_name} LIKE '" . $k . "-More'", "AND {$table}.{$field_name} >= {$k}", $where );
			} elseif ( strpos( $_REQUEST[ 's' . $field_name ], '-More-Less' ) !== false ) {
				$where_val = explode( '-More-Less', $_REQUEST[ 's' . $field_name ] );
				$where     = "AND {$table}.{$field_name} BETWEEN {$where_val[0]} AND {$where_val[1]}";
			} else {
				$l     = 7;
				$where = str_replace( "AND {$table}.{$field_name} LIKE '" . $l . "-Less'", "AND {$table}.{$field_name} <= {$l}", $where );
			}
		}
	}
	return $where;
}
add_filter( 'geodir_search_posts_where', 'gd_snippet_25052021_search_posts_where', 20, 2 );

If you have any doubt in code please ask me . I will definitely respond for it

Leave a Comment

Your email address will not be published. Required fields are marked *