Make WordPress Core

Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#21803 closed enhancement (wontfix)

Add filter to make extending searching easier

Reported by: mark8barnes's profile mark8barnes 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)

add_search_fields.patch (929 bytes) - added by mark8barnes 12 years ago.
Patch to filter search fields
add_search_fields.2.patch (933 bytes) - added by mark8barnes 12 years ago.
Revised patch incorporating scribu's correction
21803.diff (2.8 KB) - added by wonderboymusic 11 years ago.

Download all attachments as: .zip

Change History (15)

@mark8barnes
12 years ago

Patch to filter search fields

#1 @scribu
12 years ago

  • Cc azaozz added

+1 on this. Sort of related: #7394

#2 @scribu
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 @azaozz
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: @scribu
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 @mark8barnes
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.

@mark8barnes
12 years ago

Revised patch incorporating scribu's correction

#6 @toscho
12 years ago

  • Cc info@… added

#7 @scribu
12 years ago

  • Milestone changed from Awaiting Review to 3.5

#8 @bpetty
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 @wonderboymusic
12 years ago

  • Milestone changed from Future Release to 3.7

Works, cleaned up string-building and whitespace

#10 @wonderboymusic
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

#11 @wonderboymusic
11 years ago

  • Milestone 3.7 deleted
  • Resolution set to wontfix
  • Status changed from new to closed

let's move over to #7394

#12 @bhengh
11 years ago

  • Cc ben@… added
Note: See TracTickets for help on using tickets.