Support

Account

Home Forums Backend Issues (wp-admin) Query two fields and orderby another

Solved

Query two fields and orderby another

  • I’m building a directory with a ‘dmu_profile” custom post type. I want to be able to create a query that selects ‘Faculty’ and ‘Staff’ and then orders them by their ‘last_name’.

    I have a checkbox that is called ‘profile_type’ that has many options including ‘Faculty’ and ‘Staff’ and a textfield called ‘last_name’.

    Here is the query I’m trying:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array(
        'post_type' 		=> 'dmu_profile',
        'meta_key'			=> 'last_name',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'ASC',
    	'suppress_filters' => false,
    	'paged'				=> $paged,
        'meta_query' => array(
        	'relation' 		=> 'OR',
            array(
                'key' 		=> 'profile_type',
                'value' 	=> 'Faculty',
                'compare' 	=> 'LIKE'
            ),
            array(
                'key' 		=> 'profile_type',
                'value' 	=> 'Staff',
                'compare' 	=> 'LIKE'
            )
        )
    );
    
    $the_query = new WP_Query( $args );

    This results in my server hanging. What is weird is that if I remove one of the meta_query options it works:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array(
        'post_type' 		=> 'dmu_profile',
        'meta_key'			=> 'last_name',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'ASC',
    	'suppress_filters' => false,
    	'paged'				=> $paged,
        'meta_query' => array(
            array(
                'key' 		=> 'profile_type',
                'value' 	=> 'Faculty',
                'compare' 	=> 'LIKE'
            )
        )
    );
    
    $the_query = new WP_Query( $args );

    Or if I remove the ‘orderby’ the rest of the query works:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $args = array(
        'post_type' 		=> 'dmu_profile',
    	'suppress_filters' => false,
    	'paged'				=> $paged,
        'meta_query' => array(
        	'relation' 		=> 'OR',
            array(
                'key' 		=> 'profile_type',
                'value' 	=> 'Faculty',
                'compare' 	=> 'LIKE'
            ),
            array(
                'key' 		=> 'profile_type',
                'value' 	=> 'Staff',
                'compare' 	=> 'LIKE'
            )
        )
    );
    
    $the_query = new WP_Query( $args );

    Any ideas?

  • I found a WP ticket that says there’s currently an issue with meta_query and the ‘OR’ relation that I think is causing the issue – https://core.trac.wordpress.org/ticket/25538

    As a temporary solution I’m instead filtering out all non ‘Faculty’ and ‘Staff’ members by using ‘NOT LIKE’ meta_query(s):

     'meta_query' => array(
            array(
                'key' 		=> 'profile_type',
                'value' 	=> 'Adjunct',
                'compare' 	=> 'NOT LIKE'
            ),
            array(
                'key' 		=> 'profile_type',
                'value' 	=> 'Emeriti',
                'compare' 	=> 'NOT LIKE'
            ),
            (etc...)
        )
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Query two fields and orderby another’ is closed to new replies.