I have created a custom search page where i can search in a Custom Post Type (Products) based on a number of custom fields that each product post has.

This is the search input:

<form role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get" id="searchform">
   <div class="w100">
      <label for="prdsi" class="visually-hidden">Search for Products</label>
      <input class="pdg-p5 black txt-24 color-blue relative" 
             value="<?php echo esc_attr( get_search_query()); ?>"
             placeholder="<?php esc_attr_e( 'Type here' ); ?> "/>

  <label for="prdsbtn" class="visually-hidden">Search</label>
  <input id="prdsbtn" 
         class="prdsbtn h100 pointer color-transparent" 
         value="<?php esc_attr_e( 'Search' ); ?>" />

This are my wp_query arguments:

$search_term = esc_attr( get_search_query());
$args = array(
    'post_type'   => 'product',
    'post_status' => 'publish', 
    'meta_query'  => array(
        'relation' => 'OR',  
        array( "key" => "prod_needle_type", "value" => $search_term, "compare" => "LIKE" ),
        array( 'key' => 'prod_color', 'value' => $search_term, 'compare' => 'LIKE' ),
        array( 'key' => 'prod_reference_number', 'value' => $search_term, 'compare' => 'LIKE' ),
        array( 'key' => 'prod_material', 'value' => $search_term, 'compare' => 'LIKE' ),
        array( 'key' => 'prod_special_features', 'value' => $search_term, 'compare' => 'LIKE' ),
$the_query = new WP_Query($args);

Some of the custom fields contain only 1 word, so they are not a big issues, others however may contain 2 or more words.

The search terms in the URL come in the form of:

s=Blunt (one word)
s=Blunt+Point (2 words, user typed Blunt Point)

When i var_dump the $search_term, the output is string(11) "Blunt Point"

The values of the custom field with "key" => "prod_needle_type" are in a ACF select (11 different values)


When i try 2 words separated by space i simply don't get any results back.

I tried changing to a direct value like: array( "key" => "prod_needle_type", "value" => 'Blunt Point', "compare" => "LIKE" ), and i get no results. I tried changing the compare operator to "=" with array( "key" => "prod_needle_type", "value" => 'Blunt Point', "compare" => "=" ), i get no results. After experimenting and much trial and error i made a solution (following in the next section), however is is far from good-optimal.

THE BAD SOLUTION (very slow in my case because of number of custom fields + number of products)

  1. Take the search query $search_query = get_search_query();
  2. Split every term $individual_terms = explode(" ", $search_query);
  3. create an AND relation for every split word. This must be done for every ACF that may contain >2words. example:
$prodNeedleType = [];
$prodNeedleType['relation'] = 'AND';
foreach ( $individual_terms as $type ) {
   $prodNeedleType[] = [
      'key'     => 'prod_needle_type',
      'value'   => $type,
      'compare' => 'LIKE',
  1. Make an multidimensional array of every custom field array ($all_fields)
$prodColor = array( 'key' => 'prod_color', 'value' => $search_query, 'compare' => 'LIKE' );
$prodRefNum = array( 'key' => 'prod_reference_number', 'value' => $search_query, 'compare' => 'LIKE' );

$all_fields = array( 'relation' => 'OR', $prodRefNum, $prodColor, ..., ..., $prodNeedleType);
  1. arg it and wp_query it:
$args = [
    'post_type'   => 'product',
    'post_status' => 'publish', 
    'meta_query'  => $all_fields
$the_query = new WP_Query($args);


  1. Why does this array( "key" => "prod_needle_type", "value" => $search_term, "compare" => "LIKE" ) not work with multiple words in the first place? If the search worked with 2 or more words, i would not need to resort to my bad solution. i cant really explain it or understand the reason.

  2. My solution is very slow. Is there a way to speed / optimize this monstrosity up? (The results come after ~20sec of delay)

  3. Why does relation IN does not work at all?

Can someone help or should i proceed with potent medication? ^_^ Hey, thanks for your time!

RELATED THREADS / PAGES Here is a list of some of the threads i have stumbled upon:

  1. compare operators explanation
  2. related ACF thread
  3. related thread
  4. This guide
  5. related thread
  6. i based my bad solution on this
  7. space breaks search
  8. similar thread

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.