#21803 closed enhancement (wontfix)
Add filter to make extending searching easier
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.4 |
Component: | Query | Keywords: | has-patch close |
Focuses: | Cc: |
Description
I'm using custom post types, and want to extend the search function so more than post_title and post_content are searched. It is currently possible to filter posts_search
but that returns a string such as AND (((wp_posts.post_title LIKE '%test%') OR (wp_posts.post_content LIKE '%test%')))
. This string then has to be rather messily manipulated via preg_replace
or str_replace
.
It would be more useful if we could filter a list of fields to be added to the search criteria, so that we could add a function to extend or change the criteria:
add_filter ('search_fields', 'my_custom_search_fields'); function my_custom_search_fields ($fields) { global $wpdb; $fields[] = "{$wpdb->prefix}posts.post_excerpt"; return $fields; }
The attached patch implements that filter.
Attachments (3)
Change History (15)
#2
@
12 years ago
This line is incorrect though:
$search_fields = array( $wpdb->posts.post_title, $wpdb->posts.post_content );
It should be:
$search_fields = array( "$wpdb->posts.post_title", "$wpdb->posts.post_content" );
Also, I'm wondering if it should be a bit more highlevel, i.e. without the $wpdb->posts
prefixing.
#3
@
12 years ago
Right, no need for the prefix there. Also a bit unsure of the user case for this: what other fields apart from the excerpt are searchable with LIKE? If it's only for post_excerpt
perhaps it can be pre-set/limited to only title, content and excerpt. Then the filtered array can probably be:
$search_fields = array( 'post_title' => true, 'post_content' => true, 'post_excerpt' => false );
#4
follow-up:
↓ 5
@
12 years ago
what other fields apart from the excerpt are searchable with LIKE?
If you handle the JOINs, you can search through several custom fields or even term names, so maybe keeping the prefix is better.
#5
in reply to:
↑ 4
@
12 years ago
Replying to scribu:
what other fields apart from the excerpt are searchable with LIKE?
If you handle the JOINs, you can search through several custom fields or even term names, so maybe keeping the prefix is better.
Maximum flexibility is definitely needed. With the patch you can search custom meta data, terms or data stored in other custom post types (by joining tables with the posts_join_paged
filter), or even do more complicated lookups (by using the posts_fields
filter and nesting a SELECT).
One obvious real-world scenario that the filter would make possible (together with a JOIN) would be being able to extend the search at wp-admin/edit.php
to include tags.
#8
@
12 years ago
- Keywords needs-unit-tests added
- Milestone changed from 3.5 to Future Release
This missed the deadline for hard feature freeze by a week (when it was moved to 3.5), and still no review, testing, or unit tests. Punting.
#9
@
12 years ago
- Milestone changed from Future Release to 3.7
Works, cleaned up string-building and whitespace
#10
@
11 years ago
- Keywords close added; needs-testing needs-unit-tests removed
I like this, but I think it needs to be compatible with #7394 - perhaps we focus our work there
Patch to filter search fields