Meta Query in WordPress is being ignored

I wrote a custom post type called ’employees’ and now trying to query on it by calling this post type after it has shown the department page. I created a custom meta box with a couple of arguments in it, such as department, with a drop down, so people can easily assign the person to the right department. If the criteria is met, the picture of the employees (or in my case, the word crap as test) should show.

However, I seem to have a problem with trying to get the right query. My code is as following. I need to clear out stuff still, and its code in progress, so dont shoot down how it is programmed. Its just the query for now.

This is the code for the meta box

$prefix = 'shm_';
$meta_box = array(
    'id' => 'meta-employees',
    'title' => 'Overige informatie',
    'page' => 'employees',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
            'name' => 'position',
            'desc' => 'The name of the job position',
            'id' => $prefix . 'position',
            'type' => 'text',
            'std' => ''
            'name' => 'department',
            'desc' => 'Select the department this employee is working in',
            'id' => $prefix . 'department',
            'type' => 'select',
            'options' => array('Board', 'Sales, Marketing & Revenue', 'Option 3', 'Option 4', 'Option 5', 'Option 6')

And this is for the query

            if (is_page('board')){
                $page = 'board';
            }elseif (is_page('sales-marketing-revenue')){
                $page = 'Sales, Marketing & Revenue';

            echo $page;

            $i = 0;

            $args = array(
                'numberposts'   => -1,
                'post_type' => 'employees',
                'meta_query'    => array(
                    'key'     => 'department',
                    'value' => $page,
                    'compare' => '=',

                if ( have_posts() ) : 
                    while ( have_posts() ) : the_post();

                    echo "crap";

                <?php endwhile; ?>
            <?php endif; ?>
        <?php } ?>

this piece technically works, however it spits out all employees instead of the ones I set on a specific department, such as board. When I print the array everything seems to work fine.

Array ( [numberposts] => -1 [post_type] => employees [meta_query] => Array ( [key] => department [value] => Sales, Marketing & Revenue [compare] => = ) )

Yet the sales marketing & revenue have only 1 person attached to it, not 3 (the total of employees I have entered so far) which are displayed. It looks like it’s ignoring the meta query.

Anybody can help me on this?

Read more here: Meta Query in WordPress is being ignored

Leave a Reply

Your email address will not be published. Required fields are marked *