Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it more intuitive to search against title OR post meta #359

Open
danielbachhuber opened this issue Jan 22, 2018 · 2 comments
Open

Make it more intuitive to search against title OR post meta #359

danielbachhuber opened this issue Jan 22, 2018 · 2 comments

Comments

@danielbachhuber
Copy link
Contributor

If you include a custom field value in indexing options, it's easy assume this would cause the document to appear in matching search results:

image

However, getting the document to appear in search results also requires some code monkery:

<?php

/**
 * Tell WordPress to also look in the 'search_keywords' post meta for search results.
 * By default, this will be title AND post meta, which we filter to OR below.
 */
add_action( 'pre_get_posts', function( $query ) {
	if ( $query->is_search() && $query->is_main_query() ) {
		$query->set( 'meta_query', array(
			array(
				'key'     => 'search_keywords',
				'value'   => $query->get( 's' ),
				'compare' => 'LIKE',
			),
		) );
	}
});

/**
 * Filter the title AND post meta query to be title OR post meta
 */
add_filter( 'solr_select_query', function( $query ){
	$query['query'] = str_replace( 'AND((search_keywords_str:', 'OR((search_keywords_str:', $query['query'] );
	return $query;
});

This should be way more intuitive.

@jhned
Copy link
Contributor

jhned commented Dec 27, 2018

Yeah no kidding, I've been looking for this for a week.

It's also worth noting that if you're using the Query Monitor plugin, it does NOT reflect the filter change in the "Debug Bar: Solr" panel.

I'd recommend adding this to the documentation about using custom fields.

@greenSkin
Copy link

The 'pre_get_posts' seems to break search on admin pages. I added !is_admin() && !empty( $query->get( 's' ) ) to the conditional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants