Support

Account

Home Forums General Issues Query Posts by One Custom Field and 'orderby' Another Custom Field Reply To: Query Posts by One Custom Field and 'orderby' Another Custom Field

  • Thanks, John. That document was helpful.

    For someone else who may stumble across this article and find my query helpful, here it is:

    	$my_query = new WP_Query(array(
            'category_name' => 'artist',
            'posts_per_page' => -1,
            'meta_query' => array(
                'relation' => 'AND',
                'gender_clause' => array(
                    'key' => 'gender',
                    'value' => 'f',
                ),
                'sort_clause' => array(
                    'key' => 'sort_name',
                    'compare' => 'EXISTS',
                ),
            ),
            'orderby' => 'sort_clause',
            'order' => 'ASC',
        ));

    Basically, I am querying posts of the ‘artist’ category, and then selecting those posts where the ‘gender’ (a custom field I have setup) is set to the value of ‘f’. There is another custom field called ‘sort_name’ that I use to order the posts by.