07-26-2023, 10:32 PM
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.
--------
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.