Order by custom fields and name in WP Grid Builder

Snippets

January 1, 1970

This snippet came about because I needed to perform a User Query on a site utilizing GeneratePress and GenerateBlocks. There is no ability to query a list of users with the GenerateBlocks Query Loop element, so I resorted to a Grid element inside of WP Grid Builder.

At the bottom of this article, you’ll find the snippet that allows you to order your WPGB Grid results by one or more custom field values, as well as the users WordPress first & last name fields. Read on to learn more.

This grid element is set to query users with no other paramaters:

I then built out the Card in my grid to look roughly like the image below. The card layout can be setup any way you want. This took me a whole to dial in, but it pulls in dynamic data from the user and works really well for our use case.

In my case, I needed to sort the results of the Grid element in this order:

  • Content inside of an ACF text field called “team_lead_of”
  • ACF field of “account_status” set to active
  • Last Name

I couldn’t get the Grid element to sort this way in the back-end controls, so I checked the WP Grid Builder docs and found there’s a filter we can hook into.

The code I provide below essentially checks that the user’s account is active. Then, it puts anyone who is a team lead at the top of the list. Then it sorts the remaining results by last name (default WP last name field).

The snippet below modifies my grid element based on it’s ID which happens to be “1” in my case. Be sure to modify the ID to match yours.

I hope this helps!

function grid_custom_order_user_query( $query_args, $grid_id ) {

    if ( 1 === $grid_id ) {

        $meta_query = array(
            'relation' => 'AND',
            'team_lead_of_clause' => array(
                'key' => 'team_lead_of',
                'compare' => 'EXISTS'
            ),
            'account_status_clause' => array(
                'key' => 'account_status',
                'value' => 'active',
                'compare' => '='
            ),
            'last_name_clause' => array(
                'key' => 'last_name',
                'compare' => 'EXISTS'
            ),
            'first_name_clause' => array(
                'key' => 'first_name',
                'compare' => 'EXISTS'
            )
        );

        $query_args['meta_query'] = $meta_query;

        $query_args['orderby'] = array(
            'team_lead_of_clause' => 'DESC',
            'last_name_clause' => 'ASC',
            'first_name_clause' => 'ASC',
        );
    }

    return $query_args;
}

add_filter( 'wp_grid_builder/grid/query_args', 'grid_custom_order_user_query', 10, 2 );

Sources: https://docs.wpgridbuilder.com/resources/filter-grid-query-args/

Get WP Grid Builder: https://jonathanjernigan.com/go/wpgb

Join the Inside Link Community

Get access to all courses, weekly office hours, live build sessions, and an active community of WordPress professionals.

Learn More →