$_REQUEST['categories'] ?? '', 'locations' => $_REQUEST['locations'] ?? '', 'directory_type' => sanitize_text_field( $_REQUEST['directory_type'] ?? '' ), 'tags' => sanitize_text_field( $_REQUEST['tags'] ?? '' ), 'ls' => sanitize_text_field( $_REQUEST['ls'] ?? '' ), 'minPrice' => sanitize_text_field( $_REQUEST['minPrice'] ?? '' ), 'maxPrice' => sanitize_text_field( $_REQUEST['maxPrice'] ?? '' ), 'rangePrice' => sanitize_text_field( $_REQUEST['rangePrice'] ?? '' ), 'rating' => sanitize_text_field( $_REQUEST['rating'] ?? '' ), 'display_listings' => sanitize_text_field( $_REQUEST['display_listings'] ?? '' ), 'bh_status' => sanitize_text_field( $_REQUEST['bh_status'] ?? '' ), 'bh_days' => sanitize_text_field( $_REQUEST['bh_days'] ?? '' ), 'bh_open' => sanitize_text_field( $_REQUEST['bh_open'] ?? '' ), 'bh_close' => sanitize_text_field( $_REQUEST['bh_close'] ?? '' ), ]; $args = wp_parse_args( $queryArgs, $defaults ); $category = sanitize_text_field( $args['categories'] ); $location = sanitize_text_field( $args['locations'] ); $directory_type = sanitize_text_field( $args['directory_type'] ); $tags = sanitize_text_field( $args['tags'] ); $search = sanitize_text_field( $args['ls'] ); $minprice = sanitize_text_field( $args['minPrice'] ); $maxprice = sanitize_text_field( $args['maxPrice'] ); $rangePrice = sanitize_text_field( $args['rangePrice'] ); $rating = floatval( sanitize_text_field( $args['rating'] ) ); $display_listings = sanitize_text_field( $args['display_listings'] ); $custom_directory = sanitize_text_field( $args['custom_directory'] ?? '' ); // Business hour parameters $bh_status = sanitize_text_field( $args['bh_status'] ); $bh_days = sanitize_text_field( $args['bh_days'] ); $bh_open = sanitize_text_field( $args['bh_open'] ); $bh_close = sanitize_text_field( $args['bh_close'] ); $tax_query = []; $meta_query = []; // URL parameter (directory_type) always takes precedence over widget setting (custom_directory) if ( ! empty( $directory_type ) ) { // User selected a directory via URL filter $post_type = adqs_get_post_type_by_directory( $directory_type ); $post_types = ! empty( $post_type ) ? [ $post_type ] : adqs_get_directory_post_types(); } elseif ( ! empty( $custom_directory ) ) { // Widget/shortcode specified which directories to include $directory_slugs = array_filter( array_map( 'trim', explode( ',', $custom_directory ) ) ); $post_types = []; foreach ( $directory_slugs as $slug ) { $post_type = adqs_get_post_type_by_directory( $slug ); if ( ! empty( $post_type ) && post_type_exists( $post_type ) ) { $post_types[] = $post_type; } } // If no valid post types found, fall back to all if ( empty( $post_types ) ) { $post_types = adqs_get_directory_post_types(); } } else { // No directory specified, show all $post_types = adqs_get_directory_post_types(); } // Get post type from directory type $args['post_type'] = $post_types; // Only show published listings in frontend (exclude expired, draft, etc.) $args['post_status'] = 'publish'; if ( ! empty( $search ) ) { $args['s'] = sanitize_text_field( $search ); } if ( ( $minprice >= 0 ) && ( $maxprice > 0 ) ) { $meta_query[] = [ 'key' => '_price', 'value' => [ $minprice, $maxprice ], 'type' => 'NUMERIC', 'compare' => 'BETWEEN', ]; } elseif ( $minprice > 0 ) { $meta_query[] = [ 'key' => '_price', 'value' => $minprice, 'type' => 'NUMERIC', 'compare' => '>=', ]; } elseif ( $maxprice > 0 ) { $meta_query[] = [ 'key' => '_price', 'value' => $maxprice, 'type' => 'NUMERIC', 'compare' => '<=', ]; } if ( ! empty( $rangePrice ) ) { $meta_query[] = [ 'key' => '_price_range', 'value' => $rangePrice, 'compare' => '=', ]; } // Only apply rating filter if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( ! empty( $enable_review ) && ! empty( $rating ) ) { $meta_query[] = [ 'key' => 'adqs_avg_ratings', 'value' => $rating, 'type' => 'NUMERIC', 'compare' => '>=', ]; } if ( ! empty( $display_listings ) && ( $display_listings === 'featured' ) ) { $meta_query[] = [ 'key' => '_is_featured', 'value' => 'yes', ]; } adqs_search_filters_meta_query( $meta_query, $args ); // Taxonomies are now static and shared across all post types $category_taxonomy = 'adqs_category'; $location_taxonomy = 'adqs_location'; $tags_taxonomy = 'adqs_tags'; if ( ! empty( $category ) && ! empty( $category_taxonomy ) ) { $tax_query[] = [ 'taxonomy' => $category_taxonomy, 'field' => 'slug', 'terms' => explode( ',', $category ), 'operator' => 'IN', ]; } if ( ! empty( $location ) && ! empty( $location_taxonomy ) ) { $tax_query[] = [ 'taxonomy' => $location_taxonomy, 'field' => 'slug', 'terms' => explode( ',', $location ), 'operator' => 'IN', ]; } if ( ! empty( $tags ) && ! empty( $tags_taxonomy ) ) { $tax_query[] = [ 'taxonomy' => $tags_taxonomy, 'field' => 'term_id', 'terms' => array_map( 'absint', explode( ',', $tags ) ), 'operator' => 'IN', ]; } if ( count( $tax_query ) > 0 ) { if ( count( $tax_query ) > 1 ) { $tax_query['relation'] = 'AND'; } $args['tax_query'] = $tax_query; } if ( count( $meta_query ) > 0 ) { if ( count( $meta_query ) > 1 ) { $meta_query['relation'] = 'AND'; } $args['meta_query'] = $meta_query; } // Handle date queries if present if ( ! empty( $args['date_query'] ) && is_array( $args['date_query'] ) ) { // Validate date query structure $validDateQuery = true; foreach ( $args['date_query'] as $dateCondition ) { if ( ! is_array( $dateCondition ) || ! isset( $dateCondition['after'] ) ) { $validDateQuery = false; break; } } if ( $validDateQuery ) { // Ensure proper date query structure if ( count( $args['date_query'] ) > 1 ) { $args['date_query'] = [ 'relation' => 'OR', $args['date_query'], ]; } } else { // Remove invalid date query unset( $args['date_query'] ); } } return apply_filters( 'adqs_listing_query_filter_args', $args ); } /** * Method adqs_listing_query_sort_by * * @param $sort_by $sort_by [explicite description] * * @return array */ function adqs_listing_query_sort_by( $sort_by = '' ) { if ( empty( $sort_by ) ) { $sort_by = $_REQUEST['sort_by'] ?? ''; } $sortBySet = []; // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); switch ( $sort_by ) { case 'date-asc': $sortBySet['order'] = 'ASC'; break; case 'review-count': // Only allow review-count sorting if review system is enabled if ( ! empty( $enable_review ) ) { $sortBySet['orderby'] = 'comment_count'; } break; case 'rating-desc': // Only allow rating sorting if review system is enabled if ( ! empty( $enable_review ) ) { $sortBySet['meta_key'] = 'adqs_avg_ratings'; $sortBySet['orderby'] = 'meta_value_num'; $sortBySet['order'] = 'DESC'; } break; case 'title-asc': $sortBySet['orderby'] = 'title'; $sortBySet['order'] = 'ASC'; break; case 'title-desc': $sortBySet['orderby'] = 'title'; $sortBySet['order'] = 'DESC'; break; case 'price-asc': $sortBySet['meta_key'] = '_price'; $sortBySet['orderby'] = 'meta_value_num'; $sortBySet['order'] = 'ASC'; break; case 'price-desc': $sortBySet['meta_key'] = '_price'; $sortBySet['orderby'] = 'meta_value_num'; $sortBySet['order'] = 'DESC'; break; case 'rand': $sortBySet['orderby'] = 'rand'; break; case 'views-desc': $sortBySet['meta_key'] = '_view_count'; $sortBySet['orderby'] = 'meta_value_num'; $sortBySet['order'] = 'DESC'; break; } return ! empty( $sortBySet ) ? $sortBySet : []; } function adqs_search_filters_meta_query( &$meta_query = [], $args = [] ) { $checkDateTime = [ 'minute ago', 'minutes ago', 'hour ago', 'hours ago', 'day ago', 'days ago', 'week ago', 'weeks ago', 'month ago', 'months ago', 'year ago', 'years ago', ]; // Skip date-related fields as they are handled separately $skipDateFields = [ 'radio_date', 'date' ]; $directory_type = $args['directory_type'] ?? ''; $custom_directory = $args['custom_directory'] ?? ''; // Determine which directory to get custom fields from // Priority: URL parameter > Widget setting > First directory $active_directory_slug = ''; if ( ! empty( $directory_type ) ) { $active_directory_slug = $directory_type; } elseif ( ! empty( $custom_directory ) ) { // If multiple directories, use the first one for field definitions $directory_slugs = array_filter( array_map( 'trim', explode( ',', $custom_directory ) ) ); $active_directory_slug = $directory_slugs[0] ?? ''; } $dir_term = get_term_by( 'slug', $active_directory_slug, 'adqs_listing_types' ); $dir_id = ( $dir_term && ! is_wp_error( $dir_term ) ) ? $dir_term->term_id : 0; $allMetaFields = adqs_metafields_get_search_filters( $dir_id ); if ( ! empty( $allMetaFields ) && is_array( $allMetaFields ) ) { foreach ( $allMetaFields as $metaData ) { $input_type = $metaData['input_type'] ?? ''; if ( in_array( $input_type, adqs_all_custom_filter_fields() ) ) { $fieldid = $metaData['fieldid'] ?? ''; $input_type = "{$input_type}_{$fieldid}"; } $get_val = $args[ $input_type ] ?? ''; $skip = false; foreach ( $checkDateTime as $timeType ) { if ( str_ends_with( sanitize_text_field( $get_val ), $timeType ) ) { $skip = true; break; } } if ( $skip ) { continue; } // Skip date-related fields as they are handled separately if ( in_array( $input_type, $skipDateFields ) ) { continue; } // Check if this is a checkbox field (either preset or custom) if ( strpos( $input_type, 'checkbox_' ) === 0 ) { if ( ! empty( $get_val ) ) { $values = is_array( $get_val ) ? $get_val : explode( ',', $get_val ); $sub_meta = []; foreach ( $values as $val ) { $sub_meta[] = [ 'key' => "_{$input_type}", 'value' => sanitize_text_field( $val ), 'compare' => 'LIKE', ]; } if ( count( $sub_meta ) > 1 ) { $meta_query[] = array_merge( [ 'relation' => 'OR' ], $sub_meta ); } else { $meta_query[] = $sub_meta[0]; } } } elseif ( strpos( $input_type, 'select_' ) === 0 || strpos( $input_type, 'radio_' ) === 0 ) { // Handle select and radio fields if ( ! empty( $get_val ) ) { $meta_query[] = [ 'key' => "_{$input_type}", 'value' => sanitize_text_field( $get_val ), 'compare' => '=', ]; } } else { switch ( $input_type ) { case 'view_count': if ( $get_val ) { $meta_query[] = [ 'key' => "_{$input_type}", 'value' => sanitize_text_field( $get_val ), 'type' => 'NUMERIC', 'compare' => '>=', ]; } break; default: if ( $get_val ) { $meta_query[] = [ 'key' => "_{$input_type}", 'value' => sanitize_text_field( $get_val ), 'compare' => 'LIKE', ]; } } } } } } function adqs_metafields_get_search_filters( $dir_id = 0 ) { $allMetaFields = []; $all_sections = adqs_get_listing_fields( $dir_id ); if ( ! empty( $all_sections ) && is_array( $all_sections ) ) { foreach ( $all_sections as $section ) { if ( ! empty( $section['fields'] ?? null ) && is_array( $section['fields'] ) ) { foreach ( $section['fields'] as $metaField ) { if ( ! empty( $metaField['in_search'] ?? null ) ) { if ( ! empty( $metaField['input_type'] ?? '' ) ) { $allMetaFields[] = $metaField; } } } } } } return $allMetaFields; } function adqs_exlude_preset_filter_fields() { return apply_filters( 'adqs_exlude_preset_filter_fields', [ 'index', 'map', 'pricing', 'field_images', ] ); } function adqs_all_custom_filter_fields() { return apply_filters( 'adqs_all_custom_filter_fields', [ 'text', 'textarea', 'time', 'url', 'number', 'date', 'radio', 'select', 'checkbox', ] ); } function adqs_metafields_add_search_filters_html( $allMetaFields = [] ) { if ( empty( $allMetaFields ) ) { return; } if ( ! empty( $allMetaFields ) ) { if ( ! empty( array_chunk( $allMetaFields, 3 ) ?? null ) && is_array( array_chunk( $allMetaFields, 3 ) ) ) { $allFiledsColumns = array_chunk( $allMetaFields, 3 ); foreach ( $allFiledsColumns as $fieldColumns ) : ?>