search

Home  >  Q&A  >  body text

How WordPress uses meta queries to query across multiple post types

I encountered three rather complex problems. First, I have two custom post types: one for departments and one for employees. Within Employees you can select various positions/fields for each employee (labeled "Additional Roles"), but the two I needed were "Employee" and "Library".

The first query I need is all employees belonging to any department, employees belonging to employees, or employees belonging to the library. Some employees are just one of these, or a combination of all three.

The next two are slightly simpler. I only have one query for employees who are clerk only (not part of a department) or library only (also not part of a department).

I've been writing and rewriting these queries but not making any progress. I think it requires some SQL command that I can't figure out. I'll be sharing my latest iteration.

For the first query (department, employee, or library employee):

$args = array(
    'post_type'   => 'employee',
    'post_status' => 'publish',
    'posts_per_page'  => '-1',
    'orderby'   => 'meta_value',
    'meta_key'  => 'last_name',
    'order'     => 'ASC',
    'meta_query' => array(
        array(
            'key'     => 'additional_roles',
            'value'   => array('staff', 'library'),
            'compare' => 'IN',
        ),
    ),
);
$query = new WP_Query($args);

Second (Staff Only)

$args = array(
    'post_type' => 'employee',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'meta_key' => 'additional_roles',
    'meta_value' => 'staff',
);
$query = new WP_Query($args);

The query for library is the same as for staff, except that meta_value is 'library'. I know the first one shouldn't work because it's not connected to the department cpt, but don't know what to do. But I'm not sure why the other two don't work. I am currently not receiving any posts back.

I hope I've explained it well enough. I'd be happy to share more if needed. Thanks!

P粉170858678P粉170858678389 days ago471

reply all(1)I'll reply

  • P粉200138510

    P粉2001385102024-01-17 13:10:46

    posts_per_page is an INT (you provide a string)

    $args = array(
        'post_type'   => 'employee',
        'post_status' => 'publish',
        'posts_per_page'  => -1,
        'orderby'   => 'meta_value',
        'meta_key'  => 'last_name',
        'order'     => 'ASC',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'     => 'additional_roles',
                'value'   => array('staff', 'library'),
                'compare' => 'IN',
            ),
            array(
                'key' => 'department',
                'compare' => 'EXISTS',
            ),
        ),
    );
    $first_query = new WP_Query($args);
    
    $staff_args = array(
        'post_type' => 'employee',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'additional_roles',
        'meta_value' => 'staff',
    );
    $staff_query = new WP_Query($staff_args);
    
    $library_args = array(
        'post_type' => 'employee',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'additional_roles',
        'meta_value' => 'library',
    );
    $library_query = new WP_Query($library_args);

    I haven't tested this code yet. Let me know if this helps

    reply
    0
  • Cancelreply