Skip to content

Commit

Permalink
Better_Search changes
Browse files Browse the repository at this point in the history
* Use `get_match_sql` instead of `score` in the order by clause
* Support `meta_query` argument
* New filter: `better_search_query_date_query`
  • Loading branch information
ajaydsouza committed Aug 17, 2023
1 parent 6890d6a commit aaeb365
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
46 changes: 44 additions & 2 deletions includes/class-better-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,56 @@ 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' ),
'inclusive' => true,
),
);

/**
* 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'];

Expand Down Expand Up @@ -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'] ) ) {
Expand Down
7 changes: 7 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down

0 comments on commit aaeb365

Please sign in to comment.