I have posted this on StackOverflow too, not sure if that’s allowed – if not let me know and I’ll remove one (I don’t want to look like I’m spamming). Anyway….
I have a custom post type and have used the Verve Meta Boxes plugin to create some custom meta values for the post type.
The meta box is called ‘Subjects’ and has a checkbox list of subjects to assign values to it. For example, I have ticked Math, English and Science.
Now I want to perform a WP_Query which will take an array of subjects selected by the user as an array, compare those subjects to those selected for the custom post type, and return posts if any of them match.
Here’s the code so far:
$subjects_array = explode("_", $_GET["subjects"]);
$args = array(
'post_status' => 'publish',
'post_type' => 'any',
'meta_query' => array(
// Not sure what type of meta query to do
)
);
$query = new WP_Query($args);
To cut a long story short, I just need to know if there’s a way to perform a meta_query which will compare an array against whatever format the data in the meta box is in?
If this is not possible, I just need to know then I’ll look at a different way of doing it, but I’m thinking there’s perhaps some meta_query ability I don’t know about.
Any help would be much appreciated.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Take a look at the codex for a better understanding of queries of custom fields but it should look something like this:
$subjects_array = explode("_", $_GET["subjects"]);
$args = array(
'post_status' => 'publish',
'post_type' => 'any',
'meta_query' => array(
array(
'key' => 'field_name',
'value' => $subjects_array,
'compare' => 'IN'
)
)
);
$query = new WP_Query($args);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0