I'm sure this is possible, I just don't think I am going the right way about it. Basically, I have two custom post types (Investigators & Centres), both of which have search forms on their respective archive pages (below) to filter the archive based on a users input and custom fields (which are being indexed by the Custom fields search plugin).

http://dev.anklearthritis.co.uk/find-a-specialist/ (Investigators) http://dev.anklearthritis.co.uk/centres/ (Centres)

At the moment however, no matter what the user searches, the first 6 entries are always shown.

I'm using the following query (for investigators) in my search file

$args = array( 
    'post_type' => 'investigator', 
    'paged' => $paged,
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => 6, //Limits the amount of posts on each page
    'post_title' => 'LIKE %'.$_POST['s'].'%' 
$loop = new WP_Query( $args ); 

I then use a while loop to show each result on the page.

Is this even possible, and if so what am I doing wrong? Thanks

  • Search this site for pre_get_posts. I think that is probably what you want.
    – s_ha_dum
    Commented Feb 15, 2014 at 17:51
  • Could you perhaps elaborate a bit more. Would I need to create two different search filter functions for each post type?
    – jshjohnson
    Commented Feb 15, 2014 at 17:57
  • I can't put together working code for you now, but this is probably pretty close to what you need.
    – s_ha_dum
    Commented Feb 15, 2014 at 18:13
  • @s_ha_dum Thanks for your response. The code on that post refers to just restricting what is shown in search results. That would be fine if I only had one search area but I have two meaning even if I restricted all pages and posts, both post type results would show results from both post types, regardless of what page the user searches from. If that makes any sense!
    – jshjohnson
    Commented Feb 26, 2014 at 9:17

1 Answer 1


post_title isn't a valid argument to WP_Query. If you dump the SQL you will see that it is ignored:

string(189) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'investigator' AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_title ASC LIMIT 0, 6"

You can use name (or pagename) instead, but you cannot push raw SQL through like that. It won't work:

string(158) "SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'like-%d00d' AND wp_posts.post_type = 'investigator' ORDER BY wp_posts.post_title ASC "

If you use the s argument-- like this 's' => $_POST['s']--, you get something like you are looking for:

string(310) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '%d00d%') OR (wp_posts.post_content LIKE '%d00d%'))) AND (wp_posts.post_password = '') AND wp_posts.post_type = 'investigator' AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_title ASC LIMIT 0, 6"

But you are searching more fields than just the title. To restrict that to the title only, you can use a filter from another of my answers:

function only_title_search_wpse_119422( $search ) {
    remove_filter( 'posts_search', 'only_title_search_wpse_119422' );

    global $wpdb;

    $pattern = "/\({$wpdb->posts}.post_title[^)]+\)/";
    preg_match_all( $pattern, $search, $matches );

    if ( ! empty( $matches[0] ) ) {
        $search = sprintf(
            " AND (%s)",
            implode( " AND ", $matches[0] )

    return $search;
add_filter( 'posts_search', 'only_title_search_wpse_119422' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.