Opened 9 months ago

Last modified 7 months ago

#21803 new enhancement

Add filter to make extending searching easier

Reported by: mark8barnes Owned by:
Priority: normal Milestone: Future Release
Component: Query Version: 3.4
Severity: normal Keywords: has-patch needs-testing needs-unit-tests
Cc: mark8barnes, azaozz, info@…

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 (2)

add_search_fields.patch (929 bytes) - added by mark8barnes 9 months ago.
Patch to filter search fields
add_search_fields.2.patch (933 bytes) - added by mark8barnes 9 months ago.
Revised patch incorporating scribu's correction

Download all attachments as: .zip

Change History (10)

Patch to filter search fields

  • Cc azaozz added

+1 on this. Sort of related: #7394

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.

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
);

comment:4 follow-up: ↓ 5   scribu9 months 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.

comment:5 in reply to: ↑ 4   mark8barnes9 months 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.

Revised patch incorporating scribu's correction

  • Cc info@… added
  • Milestone changed from Awaiting Review to 3.5
  • 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.

Note: See TracTickets for help on using tickets.