I am trying to produce a list of users based on some parameters that are in usermeta. I have been trying to use wp_user_query and wp_meta_query with some success. I want to select everyone with a particular range of meta_key/Value (named class_of), then orderby those values ASC with a secondary sort of last names, which is also in the user_meta.
My site is running WordPress 5.9.3.
I have to admit I don’t fully understand differences between user_query and meta_query, but have been following examples from various sources online, including here.
This produces the first result correctly, selecting values in the range of 1970 to 1979 but no sorting tried:
$args = array( 'meta_key' => 'class_of', 'meta_value' => $decade, 'meta_type' => 'NUMERIC', 'meta_compare' => 'BETWEEN' ); $user_query = new WP_User_Query ($args);
$decade is 1970, 1979. That seems to work OK. I’m seeing meta_value = 1970, 1979 if I echo it out.
I have tried 30-40 changes over the last few days and read the doc and numerous searches to try to solve this, but as soon as I add any nested arrays, the query will retrieve all users. For instance this, which is presently at the above link (select 1970s) and patterned after something I saw in this forum that explained how to orderby two meta_values:
$args = array( 'meta_query' => array( 'relation' => 'AND', 'decade_clause' => array( 'meta_key' => 'class_of', 'meta_value' => $decade, 'meta_type' => 'NUMERIC', 'meta_compare' => 'BETWEEN' ), 'lname_clause' => array( 'meta_key' => 'last_name', 'meta_value' => '' ), ), 'orderby' => array( 'decade_clause' => 'ASC', 'lname_clause' => 'ASC' ), ); $user_query = new WP_User_Query ($args);
It seems like this should work, at least to select the correct years. There should only be 3 rows retrieved, as only 3 users have a ‘class_of’ meta_value in the 1970s at the moment. Most current users on my site registered before the latest forms were created. So they show up as empty rows when all users are retrieved.
I just have not been able to figure out where I’m going wrong. If anyone could point me in the right direction –
Thanks – Brian
The page I need help with: [log in to see the link]
- You must be logged in to reply to this topic.