I have an events custom post type. I have custom fields such as start date, end date, venue etc. I'm building up a custom search specifically for this post type. So far I have this


 function wpse18703_posts_where( $where, &$wp_query )
    global $wpdb;
    if ( $wpse18703_title = $wp_query->get( 'wpse18703_title' ) ) {
        $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . $wpdb->esc_like( $wpse18703_title )  . '%\'';
    return $where;
add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 );

function searchfilterevent($query) {
    $post_type = get_query_var('post_type');
        $args = array(
            'numberposts'   => -1,
            'wpse18703_title'   => 'reading festival',
            'post_type'     => 'event'
    if ($query->is_search && $post_type == 'event' ) {

return $query;


So when I go to /?s=dklfjskldfj&post_type=event I expect to see a list of results of custom post type event with the title of reading festival.

Can anyone suggest why I'm not?

  • it looks like you miss the first % after LIKE – mmm Mar 22 '16 at 14:28
  • That's fine for my example as the events I want back all start with 'reading festival' – pee2pee Mar 22 '16 at 14:32
  • I don't think wpse18703_title is a valid argument for $query, either for get or set, nor is $wpse18703_title defined so how can it equal anything. – majick Mar 22 '16 at 14:42
  • It helps to print the whole $wp_query after everyting is done and see the actual SQL-Statement if there is a problem, but it doesn't look like. I don't see a problem with wpse..._title, but I wonder about numberposts, you will need 'posts_per_page' there I guess. But I have no solution so far. – websupporter Mar 22 '16 at 14:58
  • but make sure, you still use wpdb->prepare (see codex.wordpress.org/Class_Reference/wpdb/esc_like) for security reasons. – websupporter Mar 22 '16 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.