Monday, October 17, 2022
HomeWordPress DevelopmentFast Tip: Type All Customers by Metadata

Fast Tip: Type All Customers by Metadata


For those who’re working with the All Customers display screen in WordPress and wish to kind all customers by metadata, it’s attainable with the pre_get_users hook.

In WordPress, although, there’s a handful of items of information that you just’ll must work with this hook. Particularly:

  • the meta key off of which you wish to learn the data,
  • the way you wish to order the question,
  • and the best way to arrange the meta question.

Moreover, for those who simply wish to run the question on the All Customers web page, it’s useful to stop the question from working apart from the place you need it.

Type All Customers by Metadata

First, right here’s a easy helper operate that we will use to find out if we’re on the customers.php web page inside the WordPress administration space.

/**
 * Determines if we're on the customers.php web page.
 *
 * @return bool True if we're on the customers web page.
 */
operate isOnUsersPage()
{
    return str_contains($_SERVER['REQUEST_URI'], 'customers.php');
}

Subsequent, let’s assume that our customers have a standing of being lively or being inactive. That’s, we’re sustaining their accounts however maybe they’re not lively within the system for no matter cause the applying calls.

Additional, let’s assume these are numeric values the place 0 is inactive and 1 is lively. Now we will arrange the preliminary operate so it appears one thing like this:

add_action(
    'pre_get_users',
    operate ($question) {
        if (!isOnUsersPage()) {
            return $question;
        }
    
        // TODO

        return $question;
    }
);

Notice the pre_get_users hook accepts a operate with a single argument – the question working on the web page – and that is the question that we’ll manipulate to order the customers by lively info.

The handed WP_User_Query object comprises the question variables, not but handed into SQL.

Developer Handbook

We’ll arrange an array of arrays (although it’s a single dimensional array) that signifies we’re going to make use of the standing meta key for querying customers.

After that, we’ll do the next:

  1. set the meta key on the question,
  2. assign the question within the array of arrays to the meta_query attribute.

The code ought to seem like this:

add_action(
    'pre_get_users',
    operate ($question) {
        if (!isOnUsersPage()) {
            return $question;
        }

        $metaQuery = [
            [
                'key' => 'status'
            ]
        ];
        $query->set('meta_key', 'standing');
        $query->set('meta_query', $metaQuery);

        // TODO

        return $question;
    }
);

And since we’re going to be ordering the customers in ascending order by their standing, we have to additionally specify the orderby and the order for the question:

add_action(
    'pre_get_users',
    operate ($question) {
        if (!isOnUsersPage()) {
            return $question;
        }

        $metaQuery = [
            [
                'key' => 'status'
            ]
        ];
        $query->set('meta_key', 'standing');
        $query->set('meta_query', $metaQuery);

        $query->set('orderby', 'meta_value');
        $query->set('order', 'asc');

        return $question;
    }
);

Notice that how the standing is ready will rely on the plugin you’re engaged on and whether or not or not you wish to render a column on the All Customers web page for this. And I can present that in one other tip in a future submit.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments