Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm struggling with a WordPress search function. Maybe it will just help if someone can explain to me how the built in WordPress function works. But here's what I want:

  • Two search fields, by name and by location
  • The first field should check all the common post data (only posts) + categories
  • The second field should only check on the posts custom fields
  • Only one field needs to be filled

First of all, is this even possible or do I have to use native php+sql? If it is possible, can you please give me a hint on where to start?

share|improve this question
add comment

migrated from stackoverflow.com Jan 24 '11 at 22:16

This question came from our site for professional and enthusiast programmers.

3 Answers

You can customize the search by making your own search template, and using WP_Query. In most cases there are enough parameters to do what you want, you won't need to dive into the database.

Example: creating a custom search template.

If you do need something outside of what WP_Query can do, you don't need to bust out custom php/sql you can use WordPress's WPDB class to interact with the database.

share|improve this answer
add comment

The built-in WP search looks only a Posts'/Pages' Title and Content, not thru other elements like Custom Fields and the content of Shortcodes. There's a number of plugs that will search Custom Fields:

http://wordpress.org/extend/plugins/wp-custom-fields-search/

http://wordpress.org/extend/plugins/search-everything/

http://wordpress.org/extend/plugins/relevanssi/

There also a filter apply_filters_ref_array() that "allows plugins to contextually add/remove/modify the search section of the database query."

FYI, the search is defined in query.php, and looks like:

...if ( !empty($q['s']) ) ... ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')

So title and content are the only things in the default search.

share|improve this answer
add comment

I have a solution to this problem is very simple:

Open up your search.php file, paste this code on the top of your search file.

function the_acf_search_func( $sql ){
    global $wpdb;
    $the_acf_search = get_query_var( 'the_acf_search' );
    global $s;
    if( $the_acf_search ){
            $rep = $wpdb->prepare( ") OR ( CAST(wp_postmeta.meta_value AS CHAR) LIKE '%s' )  OR (",   '%'.$s.'%' );
            $sql = str_replace(') OR (',$rep,$sql);
    }
    return $sql;
}
add_action( 'posts_where', 'the_acf_search_func' );
// var_dump($paged);die();
$args = array_merge( (array)$wp_query->query_vars, array(
    'posts_per_page'=>999999,
    // 'paged '=>$p, 
    'the_acf_search'=>$s, 
    'meta_query' => array(
       array(
           'value' => '',
           'compare' => '!=',
       )
   ) 
));

Also blogged this a little early for a better understanding... http://www.jqui.net/wordpress/wp-search-custom-fields/

hope it helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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