I have written WP_Query where I want to show custom posts list 'messages' by comparing a meta_key with an array of values(courses like either 100 or 100,200 depends on how user registered). Initially users like trainer or trainee registers to either single course(ex: 100) or multiple courses(ex: 100,200). My code is working fine for the user who registers to single course but for users who registers to multiple courses(100,200) it is showing the notifications(messages) with either 100 or 200 not showing the notifications(messages) with both the courses(100,200). like below is my code:

  $the_query = new WP_Query(array(
                    'post_type' => 'messages',
                    'meta_query' => array (
                        'relation' => 'AND',
                        array (
                            'key' => 'course',
                            'value' => $course_only_ids,
                            'compare' => 'IN'
                        array (
                            array (
                                'key' => 'inistitute_name',
                                'value' => array ($institute_name),
                                'compare' => 'IN'
                    'posts_per_page' => '5',
                    'paged' =>$paged,
                    'post_status' => $status,
                    'order' => 'DESC'

If I print_r($course_only_ids) then the array like below:

 [0] => 2031
 [1] => 1919

my meta_key 'course' is stored as a string like this 2031,300,1919 for each custom post called 'course'. I want to display notifications(messages) of both registered courses that matches with either single meta_value or both meta values. Can anyone please tell me what's wrong in the code? Thanks in advance.

  • Did you try with relation=> 'OR' ?
    – Benoti
    Nov 30, 2016 at 9:13
  • @Benoti, where to put that relation=> 'OR'? if in place of 'relation' => 'AND' in my above code then I need only 'AND' not 'OR' Dec 1, 2016 at 5:24

1 Answer 1


Since $course_only_ids is already an array, you shouldn't be nesting it inside another:

array( $course_only_ids );
// array( array( 2031, 300, 1919 ) );

Just use:

'value' => $course_only_ids,
  • I did that but It is just showing the posts with only single course value that is posts with only individual course value it is showing. I need posts which have combination of the course values also. Like above if I pass both 2031 & 1919 then posts with either 2031 or 1919 showing but I want posts with both comination also. Nov 30, 2016 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.