Support » Developing with WordPress » User Query Help

  • Resolved bdonova1

    (@bdonova1)


    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]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    WP_Meta_Query is a helper class to various query classes that get objects such as WP_User_Query. WP_User_Query gets user objects, WP_Meta_Query only gets SQL clauses for use in other queries. WP_User_Query will use WP_Meta_Query for itself if you are providing “meta_query” args.

    It’s unclear what data type $decade represents. If you can echo it out it’s probably a simple string. For use with BETWEEN it should be an array of two values. You’d want to assign $decade with something like:
    $decade = array( 1970, 1979 );

    If you’re still having trouble getting the desired results, it’s often helpful to examine the SQL that WP develops for the query. It’s saved in the query object’s $request property. You can also view all SQL that WP uses for any given HTTPS request with the Query Monitor plugin. What ever is not right about the SQL is often a good clue towards what you need to do with args to get what you want.

    Thread Starter bdonova1

    (@bdonova1)

    Thanks for the explanation on meta_query. Yes, the $decade is exactly as you indicated. I can also remove the variable and just put array(1970, 1979) and I get same behavior.
    In my first example which works to at least select the correct years, the query looks as expected in Query Monitor.
    The second example, my arguments are ignored completely, and just see this query:

    SELECT SQL_CALC_FOUND_ROWS users.*
    FROM users
    WHERE 1=1
    ORDER BY user_login ASC

    which just gives me all users.
    I followed an example here:
    I assume I have a problem with my arguments, but I don’t see it. I’d really like to get that second orderby working.

    Brian

    Moderator bcworkz

    (@bcworkz)

    I see the problem now! You have ‘meta_key’ and ‘meta_value’ args within ‘meta_query’ array. Should just be ‘key’ and ‘value’! The former are for single meta data args outside of ‘meta_query’. For example you need:

       		'lname_clause'  => array(
        		    'key'     => 'last_name',
                        'value' => ''
                            ), 
    Thread Starter bdonova1

    (@bdonova1)

    Yes, that was the main problem. It gave me a better looking query in Query Monitor, but initially gave me no results. Looking at the resulting query led me to believe the problem was in the secondary orderby. The problem was this:

    'lname_clause'  => array(
        			'key'     => 'last_name',
                            'value' => ''

    I thought I needed a value =''

    Just removed the value= ” under the lname_clause and now getting proper results and sorting.
    Thanks for the help!!
    Brian

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.