Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 253 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
meta_query, how to search using both relation OR & AND?

#1
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.
Reply

#2
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'

)
)
);
Reply

#3
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through