Skip to content

Commit

Permalink
Fix a bug when searching for post titles didn’t work in combination w…
Browse files Browse the repository at this point in the history
…ith meta value searches
  • Loading branch information
gchtr committed Jul 29, 2020
1 parent d0fc55f commit 8f68add
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.5.2 - 2020-07-29

- Fixed a bug when wrong translation loaded in admin.
- Fixed a bug when searching for post titles didn’t work in combination with meta value searches.
- Fixed some code style issues (thanks, @szepeviktor).

## 2.5.1 - 2020-07-08
Expand Down
56 changes: 50 additions & 6 deletions lib/Post_Type_Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function init() {
], 10, 2 );

if ( is_admin() ) {
add_action( 'pre_get_posts', [ $this, 'search_custom_fields' ] );
add_action( 'pre_get_posts', [ $this, 'search_meta_or_title' ] );
add_action( 'pre_get_posts', [ $this, 'sort_by_meta' ] );
}
}
Expand Down Expand Up @@ -201,18 +201,27 @@ public function column_content( $column_name, $post_id ) {
*
* @param \WP_Query $query WordPress query object.
*/
public function search_custom_fields( \WP_Query $query ) {
public function search_meta_or_title( \WP_Query $query ) {
global $typenow;
$searchterm = $query->query_vars['s'];

if ( ! $query->is_main_query() || $typenow !== $this->post_type || empty( $searchterm ) ) {
if (
! $query->is_main_query()
|| $typenow !== $this->post_type
|| empty( $searchterm )
) {
return;
}

$meta_columns = array_filter( $this->columns, function( $column ) {
return 'meta' === $column['type'] && $column['searchable'];
} );

// Bail out and use default search if no searchable meta columns are defined.
if ( empty( $meta_columns ) ) {
return;
}

$meta_query = [ 'relation' => 'OR' ];

foreach ( $meta_columns as $key => $column ) {
Expand All @@ -223,12 +232,47 @@ public function search_custom_fields( \WP_Query $query ) {
];
}

$query->set( 'meta_query', $meta_query );

/**
* The search parameter needs to be removed from the query, because it will prevent
* the proper posts from being found.
* Remove search parameter.
*
* The search parameter needs to be removed from the query, otherwise posts can’t be found.
* A disadvantage of this is that all the logic in \WP_Query::parse_search() is lost.
*
* @see \WP_Query::parse_search()
*/
$query->set( 's', '' );

$query->set( 'meta_query', $meta_query );
// Fixes the "Search results for …" label in the post list table.
add_filter( 'get_search_query', function( $query ) use ( $searchterm ) {
return $searchterm;
} );

/**
* Update meta query to include search for title.
*
* This logic is taken from a StackOverflow answer:
* @link https://wordpress.stackexchange.com/a/178492/22506
*/
add_filter( 'get_meta_sql', function( $sql ) use ( $searchterm ) {
global $wpdb;

// Run only once.
static $nr = 0;

if ( 0 != $nr ++ ) {
return $sql;
}

// Modify the WHERE part.
$sql['where'] = sprintf(
" AND ( %s OR %s ) ",
$wpdb->prepare( "{$wpdb->posts}.post_title like '%%%s%%'", $searchterm ),
mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
);

return $sql;
} );
}
}

0 comments on commit 8f68add

Please sign in to comment.