From aaeb365843d574f1ae34f2b9512045510e823d57 Mon Sep 17 00:00:00 2001 From: Ajay D'Souza Date: Thu, 17 Aug 2023 21:29:41 +0100 Subject: [PATCH] Better_Search changes * Use `get_match_sql` instead of `score` in the order by clause * Support `meta_query` argument * New filter: `better_search_query_date_query` --- includes/class-better-search.php | 46 ++++++++++++++++++++++++++++++-- readme.txt | 7 +++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/includes/class-better-search.php b/includes/class-better-search.php index 2da7186..74c8adc 100644 --- a/includes/class-better-search.php +++ b/includes/class-better-search.php @@ -265,7 +265,7 @@ public function prepare_query_args( $args = array() ) { $args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query // Set date_query. - $args['date_query'] = array( + $date_query = array( array( 'after' => ( 0 === absint( $args['how_old'] ) ) ? '' : gmdate( 'Y-m-d', strtotime( current_time( 'mysql' ) ) - ( absint( $args['how_old'] ) * DAY_IN_SECONDS ) ), 'before' => current_time( 'mysql' ), @@ -273,6 +273,48 @@ public function prepare_query_args( $args = array() ) { ), ); + /** + * Filter the date_query passed to WP_Query. + * + * @since 3.2.2 + * + * @param array $date_query Array of date parameters to be passed to WP_Query. + * @param array $args Arguments array. + */ + $args['date_query'] = apply_filters( 'better_search_query_date_query', $date_query, $args ); + + // Meta Query. + if ( ! empty( $args['meta_query'] ) && is_array( $args['meta_query'] ) ) { + $meta_query = $args['meta_query']; + } else { + $meta_query = array(); + } + + /** + * Filter the meta_query passed to WP_Query. + * + * @since 3.2.2 + * + * @param array $meta_query Array of meta_query parameters. + * @param array $args Arguments array. + */ + $meta_query = apply_filters( 'better_search_query_meta_query', $meta_query, $args ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + + // Add a relation key if more than one $meta_query. + if ( count( $meta_query ) > 1 ) { + /** + * Filter the meta_query relation parameter. + * + * @since 3.2.2 + * + * @param string $relation The logical relationship between each inner meta_query array when there is more than one. Default is 'AND'. + * @param array $args Arguments array. + */ + $meta_query['relation'] = apply_filters( 'better_search_query_meta_query_relation', 'AND', $args ); + } + + $args['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + // Set post_status. $args['post_status'] = empty( $args['post_status'] ) ? array( 'publish', 'inherit' ) : $args['post_status']; @@ -716,7 +758,7 @@ public function posts_orderby( $orderby, $query ) { } if ( ! empty( $this->use_fulltext ) ) { - $orderby = ' score DESC '; + $orderby = ' ' . $this->get_match_sql( $this->search_query, $this->query_args ) . ' DESC '; } if ( ! empty( $this->query_args['bydate'] ) ) { diff --git a/readme.txt b/readme.txt index 2610899..10b71fc 100644 --- a/readme.txt +++ b/readme.txt @@ -97,6 +97,13 @@ Know of a better profanity filter? Suggest one in the [forums](https://wordpress == Changelog == += 3.2.2 = + +* Enhancements: + * Use `get_match_sql` instead of `score` in the order by clause + * Support `meta_query` argument + * New filter: `better_search_query_date_query` + = 3.2.1 = Release post: [https://webberzone.com/blog/better-search-v3-2-0/](https://webberzone.com/blog/better-search-v3-2-0/)