Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Trying to build a search that not only searches the defaults (title, content etc) but also a specific custom field.

My current query:

$args = array(
  'post_type' => 'post',
  's' => $query,
  'meta_query' => array(
     array(
       'key' => 'speel',
       'value' => $query,
       'compare' => 'LIKE'
     )
   )
);

$search = new WP_Query( $args )
...

This returns posts which match both the search query AND the meta query, but I would also like it to also return posts where it simply matches either one of them.

Any ideas?

share|improve this question

4 Answers

What you are wanting to do isn't possible using just one search query. You would need to run both queries separately and then merge them together. This has been described at this other answer. It should give you a hand with how to do it.

Can i merge 2 new WP_Query($variable) 's?

share|improve this answer
Thanks for the push in the right direction mate, I'll post up my answer albeit not as elegant as what I had hoped. – luke Jan 8 at 6:15

Did you happen to get this working based of the idea of merging two separate queries? Im wondering how this would post order and relevance of the search.

I am attempting to do the exact thing as you.

-d

share|improve this answer
check out my supplied answer, that should put you on the right track! – luke Jan 8 at 22:38
Thanks! This is working when building the array, but for some reason when passing $unique to WP_Query it is not returning any results. $args = array( 'paged' => $paged, 'post__in' => $unique, 'posts_per_page' => $num_posts, ); Any idea? – Darren Cooney Jan 8 at 23:25
Actually it is working, thanks so much. For some reason WP_Query was forcing my to set 'post_type' even though I already had the IDs. Thanks again! – Darren Cooney Jan 8 at 23:28
Sorry forgot to mention that it required a post type. Glad it works! – luke Jan 9 at 0:49
up vote 0 down vote accepted

As per Nick Perkins' suggestion, I had to merge two queries like so:

$q1 = get_posts(array(
        'post_type' => 'post',
        's' => $query
));

$q2 = get_posts(array(
        'post_type' => 'post',
        'meta_query' => array(
            array(
               'key' => 'speel',
               'value' => $query,
               'compare' => 'LIKE'
            )
         )
));

$merged = array_merge( $q1, $q2 );

$post_ids = array();
foreach( $merged as $item ) {
    $post_ids[] = $item->ID;
}

$unique = array_unique($post_ids);

$posts = get_posts(array(
    'post_type' => 'posts',
    'post__in' => $unique,
    'post_status' => 'publish',
    'posts_per_page' => -1
));

if( $posts ) : foreach( $posts as $post ) :
     setup_postdata($post);

     // now use standard loop functions like the_title() etc.     

enforeach; endif;
share|improve this answer

Well its kind of a hack but it works. You need to add posts_clauses filter. This filter function check for the any of the query word exists in the custom field "speel" and the remaining query stays intact.

function custom_search_where($pieces) {

    // filter for your query
    if (is_search() && !is_admin()) {

        global $wpdb;

        $keywords = explode(' ', get_query_var('s'));
        $query = "";
        foreach ($keywords as $word) {

            // skip possible adverbs and numbers
            if (is_numeric($word) || strlen($word) <= 2) 
                continue;

            $query .= "((mypm1.meta_key = 'speel')";
            $query .= " AND (mypm1.meta_value  LIKE '%{$word}%')) OR ";
        }

        if (!empty($query)) {
            // add to where clause
            $pieces['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$query} ((wp_posts.post_title LIKE '%", $pieces['where']);

            $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
        }
    }
    return ($pieces);
}
add_filter('posts_clauses', 'custom_search_where', 20, 1);
share|improve this answer

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.