1. Thank you – great code!

    I also had the problem:
    When the search term ($keyword) is not found,
    the code outputs all posts as found (I still do not know why!?).

    I wanted to check if results are found or not.
    If results are found: Output the results.
    If no results are found: Output “Sorry, no results found”

    My solution:
    Look if there are entries in the array &args in the field ‘post__in’

    if (!empty($args[‘post__in’]) )
    { // query, output }
    else {echo ‘Sorry, no results found.’;}

    -) What do you think?
    -) Why outputs the original code all posts as found if there are no posts found?
    -) How to implement a “exact phrase search” – like “sentence=1” in WP search?

  2. Hi! Thank you for share it, is a very usefull script. Works fine.
    But I have a issue: I did a custom search form, with my taxonomy through ‘select’, part of the code:
    Seleccionar Colecciones
    $theterms = get_terms('coleccion', 'orderby=name');
    foreach ($theterms as $term) :
    echo "slug.”‘”.($_GET[‘coleccion’] == $term->slug ? ‘ selected=”selected”‘ : ”).”>”.$term->name.”\n”;

    I tried deleting the part of $_GET and changing it, but doesn’t work if I use my select for filter the search, instead only works as a normal search (input text, etc)
    How I can do for to use the select filter properly?

    Thanks for any help!

  3. This has been a great help allowing me to extend the functionality of something I’ve been working on.

    With that said, I’ve hit a snag that I can’t seem to resolved.

    The problem:

    If someone searches:

    – a blank search, or
    – a search that has no possible results (something stupid, like ‘skjdhfksdhasighudfglkjsdflkjgsdfg’)

    The results return my home page.

    This wouldn’t be a problem if I could tell it to return something different but for the life of me I can’t figure it out.

    I believe this is because of the way the query is being determined, straying from the core WordPress functionality (which is understandable given the functionality this code is providing).

    What I’d like:

    At the very basic I just need a way to determine if the user search something that was a) blank, or b) no matches were returned. From here I can do what I need to do. I’m OK with it showing my home page, I just need to be able to return a message letting the user know that the search was blank or unmatched.

    What I’m working on can be found here: http://ml.utility.bz (although it is slow at the moment as it isn’t optimized yet).

    Not sure if anyone has run into any similar issues.

  4. Hi, I’ve been looking for ages for a good way to do this but I’m a bit of a beginner when it comes to wordpress and php in general so I was wondering if you could tell me where this needs to go in search.php? Probably a really stupid question but I just can’t work it out!


  5. Hey, this is really brilliant but it only works while searching for 1 word. If i search for 2 or more words no results are found.

    Any Ideas why?

  6. Thanks a lot of this. I couldn’t get it to work at first but that was because I was expecting to find results from within my custom posts types – the code above only searches posts. Just had to change the first line in the $args array to something like this:

    'post_type' => array('page', 'post', 'solution'),

    Thanks again!

  7. How would you search all the post meta but not search one specific meta_key value?

    For example, you have 20 custom fields – you only want to search 19 of the values of those custom fields.

  8. This is really helpful, thank you. I’ve got this working perfectly except that I can’t get pagination to work with this. No matter do, anything beyond page 1 give pulls up no posts even though there are some. I am using kriesi_pagination but even if I swap that out with the default WP pagination, it doesn’t pull up anything. Any thoughts on how I might get this to work? Thanks again for a great post.

    • I’m having exactly the same problem. I can’t get pagination to work. Even if the number of pages are displayed correctly, posts_nav_link are not showing.

      Thanks for this post, it has been really helpful :)

      • HI.

        Did anyone manage to get the pagination to work? If yes, please share how.



        • The reason I was having a problem is because I was using this in the WordPress search page and the search parameter was causing the issue. I created a new custom search page and used this query as my custom search and the pagination issue was resolved. It’s been a while, so I don’t remember all the details, but that was the basic issue.

          I hope it helps.

  9. Hi !!
    That solution is what I was looking for since 2 weeks.
    I need to create a custom fields hand made query to check in 28 fields.
    Is not for a real state site but something with a similar custom search in fields form.
    I understand that this way is best than wp meta_query because it can’t solve my problem due mysql dead each time i launch the multiple custom fields query using meta_query.

    I’m not using WordPress search form. I’m using a custom form and the search page recibe values to ask for.
    But I have a problem with that query:

    $post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
    SELECT DISTINCT post_id FROM {$wpdb->postmeta}
    WHERE 1 = 1 // optional
    AND (meta_key = 'type' AND meta_value LIKE '$f_type')
    AND (meta_key = 'morphology' AND meta_value LIKE '$f_morphology')
    " ) );

    Work’s fine if I only ask for one meta, no matter what meta but if I ask for more than one meta it isn’t works.

    Any help please? :(

    Sincerely thanks for show me the way :)

    • Looks like your SQL statement is not correct. You should use LIKE ‘%something%’ instead of LIKE ‘something’.

      You can look at my code to see how I implemented it:

      $f_type= '%' . like_escape( $f_type ) . '%';

      • Hi rilwis!

        Sorry, I didn’t explain that before that code I prepare the vars ($) to use in Like forms :P

        $f_type = “%”.like_escape( $f_type ).”%”;

        Even try with directly values like :

        AND (meta_key = 'type' AND meta_value LIKE '%Normal%')

        but with same results :(

        Thanks rilwis!

        • Just looked at that again and found that the conditions are not correct. You use AND but it actually OR, like this:

          WHERE 1=1 AND ( (condition_for_meta1) OR (condition_for_meta2) )

          Or you better remove 1=1 to make the conditions clearer and you’ll see how it works.

  10. You may need to use the like_escape function to sanitize your keyword. Can never be too careful:

    $keyword = “%”.like_escape( $keyword ).”%”;

  11. In fact, there is a workaround that doesn’t require any plugins or scripting. If you copy the content of your Custom Fields at the end of the Post text and COMMENT it (), WordPress standard Search function will sill find it! ;-)

  12. I copied and pasted your 2nd code into deafault template twentyeleven search.php file but it wont work. Can you explain please?


Leave a Reply