i have users with 2 custom meta fields. Vip (0 or 1) and likes (numeric)
I need to query users with orderby them by Vip and likes, so i need that first was vip users, than users with max likes.
I have such code
$meta_query = array(
'relation' => 'OR',
'vip_clause' => array(
'key'=> 'vip',
'compare' => 'EXISTS' ,
'type' => 'NUMERIC' ,
),
'like_clause' => array(
'key'=> 'al_likes',
'type' => 'NUMERIC',
'compare' => 'EXISTS' ,
),
array(
'key'=> 'vip',
'compare' => 'NOT EXISTS' ,
),
array(
'key'=> 'al_likes',
'compare' => 'NOT EXISTS' ,
),
);
$args = array(
'meta_query' => $meta_query,
'orderby' => array(
'like_clause' => 'DESC',
'vip_clause' => 'DESC'
)
);
$users = new WP_User_Query ( $args );
As a result i get lists of users which are ordered by Vip (them first) but not by likes too. How can i fix it? Thank you
The "orderby" parameter of WP_User_Query doesn't support array value. I guess you took the sample from WP_QUERY, which is for POST data.
Try using simple, comma separated string, instead of array. WP_USER_QUERY can recognize it.
$args = array(
'meta_query' => $meta_query,
'order' => 'DESC,DESC',
'orderby' => 'like_clause,vip_clause',
);
Related
I'm creating a one-way relationship between two products, with the 'linked-to' product outputting the product information of the 'linked-from' product.
I'm able to use a custom field to create the relationship, and use a meta query to generate the results based on this field, but I also need to check that the 'linked-from' product is still either In Stock (if not being stock managed), or has more than 0 as a stock quantity.
This is the approach I have taken so far, which returns no matching results (when indeed there are matches):
$the_query = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_key' => 'exact_new_version',
'meta_value' => $product->get_id(),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_stock_status',
'value' => array('instock'),
'compare' => 'IN',
),
array(
'key' => '_stock_quantity',
'value' => 0,
'compare' => '>'
)
)
));
The alternative is to check the stock of the returned match(es) in the query loop, but I imagine this to be far less efficient.
I've managed to achieve the desired result by passing in two sets of associative arrays into the meta query with an AND relationship, one of which (checking stock) containing a further two nested assoc. arrays with an OR relationship.
Essentially, the meta value has to match either an 'instock' item (if stock control isn't being used), or an item with '> 0' in stock, if it is:
$the_query = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' =>
array(
'relation' => 'AND',//*
array(
'key' => 'new_version',
'value' => $product->get_ID(),
'compare' => 'LIKE',
),
array(
'relation' => 'OR', //**
array(
'key' => '_stock_status',
'value' => array('instock'),
'compare' => 'IN',
),
array(
'key' => '_stock_quantity',
'value' => 0,
'compare' => '>',
),
),
),
));
How can I show user and order them by 2 meta fields? For example I have user query
$args = array(
'role' => 'specialist',
'meta_key' => 'proffession',
'meta_value' => $term->ID,
'meta_compare' => '=',
'number' => 20,
'paged' => $paged,
);
$user_query = new WP_User_Query($args);
I want to sort this query by two meta_values, for example in PHP:
if(get_usermeta( $user_id, $meta_key = 'be_first' )>time() AND get_usermeta( $user_id, $meta_key = 'pro_date' )>time())
// show this users first ordered by be_first DESC, ordered by pro_date
if($pro_date>time()){
//show this users second ordered by pro_date desc
}
//show all other users
You can try this... I don't have your fields, so this is not tested. You can create a meta_query where your clauses are set to compare exists then use those to orderby.
$args = array(
'role' => 'specialist',
'number' => 20,
'paged' => $paged,
'meta_query' => array (
'relation' => 'AND',
array(
'key' => 'profession',
'value' => '$term->ID',
'compare' => '=',
),
'be_first_clause' => array(
'key' => 'be_first',
'compare' => 'EXISTS',
),
'pro_date_clause' => array(
'key' => 'pro_date',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'be_first_clause' => 'ASC',
'pro_date_clause' => 'ASC'
)
);
$user_query = new WP_User_Query($args);
I need some help getting a wp_user_query to work; been struggling and trying lots of stuff but cannot get it to work proper way.
here is my code:
$args = array(
'meta_query' => array(
'role' => 'personal-injury-lawyer',
'orderby' => 'meta_value',
'meta_key' => 'lawyer_numeric_rank',
'order' => 'ASC',
array(
'key' => 'lawyer_location',
'value' => 'London',
'compare' => '='
),
)
);
$london_user_query = new WP_User_Query($args);
if ( ! empty($london_user_query->results)) {
foreach ( $london_user_query->results as $user ) {
I know the foreach loop is not closed; just trying shorten this...
I am trying to do three things on this query:
Show roles of personal-injury-lawyer
Show where meta_key field of lawyer_location = London
Order by meta_value where meta_key = lawyer_numeric_rank in ASC order
I have tried this a number of ways but cannot get this to work with both filtering on London location and also ordering by the rank...
The code right now does filter on location; but the orderby part is not working; if i remove the location filter from this; then the orderby does work...
I hope someone can help with this.
you were forming the meta query argument incorrectly. The below code should help you with quite a few more meta queries as well.
$args = array(
'role'=> 'personal-injury-lawyer', //assuming you have created a role that corresponds..
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'lawyer_numeric_rank',
'meta_query' => array(
// 'relation'=> 'AND', --> if you want more conditions
array(
'key' => 'lawyer_location',
'value' => 'London',
'compare' => '='
),
/* even more conditions with OR, cumulative effect is match lawyer location AND 1 of the nested arrays
array(
'relation' => 'OR',
array(
'key' => '',
'value' => '',
'compare' => '=',
),
array(
'key' => '',
'value' => '',
'compare' => '=',
),
),
*/
)
);
I have a custom field in my users field, called order. It's an input field where I can add numbers that indicate the order in which my users should appear in the query. I also have a custom field that indicates a level of a user (Worker, Support, Outside help...)
I have arguments for the query set up like:
$args = array(
'meta_query' => array(
0 => array(
'key' => 'user_select',
'value' => 'worker',
),
1 => array(
'key' => 'order',
)
),
'orderby' => 'order'
);
$user_query = new WP_User_Query( $args );
but this is not working. The worker one is working (I get users assigned only to workers value), but the order is not working. How can I make it so that the query will output users according to numbers (in ascending order).
Found the solution!
$args = array(
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'order',
),
array(
'key' => 'user_select',
'value' => 'worker',
)
),
);
Seems to work :)
I'm attempting to output a list of WordPress users using new WP_User_Query( $args ). The list is made up only of 'Special Members' and 'Superspecial Members':
I am currently using the following 'OR' relation meta_query as my $args to achieve this:
// the Args
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'access',
'value' => 'Special Member',
'compare' => '=',
),
array(
'key' => 'access',
'value' => 'Superspecial Member',
'compare' => '=',
),
)
);
I'd also like to order the outputted list by user last name (as below). Is there some way to add this to the above set of args as an 'And' relation? I have tried appending to the 'Superspecial Member' array, but this breaks the code.
$args = array(
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'last_name',
);
Any ideas?
Don't you want to just do something like this?
$args = array(
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'last_name',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'access',
'value' => 'Special Member',
'compare' => '=',
),
array(
'key' => 'access',
'value' => 'Superspecial Member',
'compare' => '=',
)
)
);