31

Resolved: See answer below.


I have a custom post type called BOOKS. It has several custom fields, named: TITLE, AUTHOR, GENRE, RATING. How do I fix my meta_query code below so that only books that have the search word in the custom fields: title, author, genre WITH EXACTLY the rating specified in my search form, gets displayed in the results?

I have made a custom search form; a text area that will search through the title, author and genre; and a dropdown that will search for the rating. The meta_query I made below only searches through the title, author, and genre. But I am now stumped in how to add the code for the rating.

This is how I visually imagined it with meta_query relation: (title OR author OR genre) AND rating

$args = array(
        'relation' => 'OR',
          array(
             'key' => 'title',
             'value' => $searchvalue,
             'compare' => 'LIKE'
          );
          array(
             'key' => 'author',
             'value' => $searchvalue,
             'compare' => 'LIKE'
          );
          array(
             'key' => 'genre',
             'value' => $searchvalue,
             'compare' => 'LIKE'
          );
), 
        array(
        'relation' => 'AND', 
          array(
             'key' => 'rating',
             'value' => $ratingvalue,
             'compare' => '=',
             'type' => 'NUMERIC'
          ));

I would extremely appreciate your help and advice.

Mario Boss
  • 819
  • 3
  • 10
  • 36
Angelica Santibanez
  • 1,081
  • 1
  • 8
  • 10

2 Answers2

64

I found the solution with some help. The code below worked perfectly.

    $args => array(
        'relation' => 'AND',
        array(
            'relation' => 'OR',
            array(
                'key' => 'title',
                'value' => $searchvalue,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'author',
                'value' => $searchvalue,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'genre',
                'value' => $searchvalue,
                'compare' => 'LIKE'
            )
        ),
        array(
            'key' => 'rating',
            'value' => $ratingvalue,
            'compare' => '=',
            'type' => 'NUMERIC'

        )
    )
);
Angelica Santibanez
  • 1,081
  • 1
  • 8
  • 10
  • 2
    This shouldn't work – it isn't supported by WP_Meta_Query. See http://wordpress.stackexchange.com/questions/75079/nested-meta-query-with-multiple-relation-keys – markd Dec 17 '13 at 01:58
  • Yes, it is actually not working. Need a permanent solution to for using both relation. This is very needy. – dipak_pusti Jul 14 '14 at 12:52
  • 3
    In WP 4.1, nested meta queries like this answer should now work, more info here: https://make.wordpress.org/core/2014/10/20/update-on-query-improvements-in-4-1/ – mfink Jan 08 '15 at 23:45
  • @Angelica Santibanez digging up an old topic I know but is this officially supported now? I am struggling to get this working and hope you can help! – Chris Aug 19 '15 at 15:22
  • Worked like a charm! Thanks! – djspark Jan 15 '18 at 17:01
  • 2
    Just to add to an old answer: now it is clearly supported and explained in the codex: https://codex.wordpress.org/Class_Reference/WP_Meta_Query#Initializing_WP_Meta_Query – Erenor Paz Feb 08 '18 at 09:38
5

After a bit trial and error I find a solution to this. This is logical I mean meta_query does not supports the array for field "key", but giving the array in "key", I'm getting the perfect solution. I may be sound crazy but it's working like charm.

$args => array(
    'relation' => 'AND',
    array(
        'key' => array('title','author','genre',),
        'value' => $searchvalue,
        'compare' => '='
    ),
    array(
        'key' => 'rating',
        'value' => $ratingvalue,
        'compare' => '=',
        'type' => 'NUMERIC'

    )
)

You only get a warning for "trim()" as we are passing an array instead of a string. Suppress that warning or Please add something if you find a better solution.

dipak_pusti
  • 1,399
  • 2
  • 19
  • 37
  • I was using this key => array() hack, however in WP 4.1 it appears to no longer be 'unofficially' supported. Fortunately the 4.1 updates to meta_query now allow the chosen answer to work properly. – mfink Jan 08 '15 at 23:43