1
votes

I want a query that returns CPT posts that contains the user's input (which is $text).

Inserting meta_key and meta_value works well but putting it in a meta_query array doesn't return anything. I want to be able to search in multiple custom fields. It's a theme I made from scratch so there is no plugins and the functions.php file is pretty small so I don't think it could be a conflict.

Code for meta_key and meta_value in the query declaration (working):

$searchquery = new WP_Query( 
    array( 'post_type' => 'offre',
           'meta_key' => 'offre_employeur',
           'meta_value' => $text, 
           'meta_compare'=> 'LIKE' ) 
);

Code for meta_value array (not working):

$meta_query_args = array(
    'relation' => 'OR',
    array(
        'key'     => 'offre_employeur',
        'value'   => $text,
        'compare' => 'LIKE'
    ),  array(
        'key'     => 'offre_titre',
        'value'   => $text,
        'compare' => 'LIKE'
    )
);
$searchquery = new WP_Query(array('post_type' => 'offre'));
$searchquery->set('meta_query', $meta_query_args);

Code for the second way I tried but still no results (not working)

     $args = array(
       'post_type' => 'offre',
        'posts_per_page' => -1,
        's' => $text,
        'meta_query' => array(
            array(
                'key' => 'offre_employeur',
                'value' => $text,
                'compare' => 'LIKE'
            ),
              array(
                'key' => 'offre_titre',
                'value' => $text,
                'compare' => 'LIKE'
            )
        )
    );


    $searchquery = new WP_Query($args);

Thank you in advance for your time.

1
And you are confident the value of $text is the same in each case?random_user_name
@cale_b yes. I even tried with a static value like 'value' => 'chef', knowing that 'chef' should be returning something.JulesHopes
Also, your last example - that one is most likely to work, however I don't understand why you've included the 's' => $text parameter. Additionally, you don't have the relation argument in that last query? Add the relation argument, and remove the 's' argument - does it return the desired results?random_user_name
@cale_b YES it works! Was wordpress confused by me adding a value to 's' ?JulesHopes
I think it needed the OR (default is AND), and yes, the s argument searches title and content, but not meta, so was almost certainly adding issues.random_user_name

1 Answers

2
votes

Per the docs here: https://codex.wordpress.org/Class_Reference/WP_Query

I would assert you want to scroll down to the section titled "Multiple Custom Field Handling:", which has this example, which most closely matches your situation:

$args = array(
    'post_type'  => 'product',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'color',
            'value'   => 'blue',
            'compare' => 'NOT LIKE',
        ),
        array(
            'key'     => 'price',
            'value'   => array( 20, 100 ),
            'type'    => 'numeric',
            'compare' => 'BETWEEN',
        ),
    ),
);
$query = new WP_Query( $args );

Which, taken what you've provided in your question, I would modify as follows to get the results you are after:

$args = array(
       'post_type' => 'offre',
        'posts_per_page' => -1,
        // remove the "search" query, which restricts the results to those with titles / content that match the $text content
        // 's' => $text,
        'meta_query' => array(
            // add the "relation" argument, default is AND, you need OR
            'relation' => 'OR',
            array(
                'key' => 'offre_employeur',
                'value' => $text,
                'compare' => 'LIKE'
            ),
              array(
                'key' => 'offre_titre',
                'value' => $text,
                'compare' => 'LIKE'
            )
        )
    );


    $searchquery = new WP_Query($args);