WordPress WP_User_Query - php

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);

Related

How to filter WP_Query with 'orderby' (ASC & DESC) with meta_query?

I'm trying to filter posts by 'DESC' with custom meta: 'like_count_on_post', then collecting all empty 'like_count_on_post' with 'dislike_count_on_post', and then finishing with 'ASC' of 'dislike_count_on_post', but I only get likes descending, or if I remove:
'custom_field_value' => 'DESC'
I can get ascending of dislikes, but not both.
The query arguments code:
$args = array(
'post_status' => 'publish',
'post_type' => 'sveikinimai',
'meta_query' => array(
"relation" => "and",
'likes' => array(
"relation" => "or",
'custom_field_value' => array(
'key' => '_like_count_on_post_',
),
'custom_field' => array(
'key' => '_like_count_on_post_',
'compare' => 'NOT EXISTS',
),
),
'dislikes' => array(
"relation" => "or",
'custom_field_value_2' => array(
'key' => '_dislike_count_on_post_',
),
'custom_field_2' => array(
'key' => '_dislike_count_on_post_',
'compare' => 'NOT EXISTS',
),
),
),
'orderby' => array(
'custom_field_value' => 'DESC',
'custom_field_value_2' => 'ASC'
),
'posts_per_page' => 20,
'paged' => $paged,
);
Update, here is code if you want to filter without non existing meta fields:
$args = array(
'post_status' => 'publish',
'post_type' => 'sveikinimai',
'meta_query' => array(
"relation" => "and",
'custom_field_value' => array(
'key' => '_like_count_on_post_',
),
'custom_field_value_2' => array(
'key' => '_dislike_count_on_post_',
),
),
'orderby' => array(
'custom_field_value' => 'DESC',
'custom_field_value_2' => 'ASC'
),
'posts_per_page' => 20,
'paged' => $paged,
);
To solve problem, I added to all posts 'like_count_on_post' and 'dislike_count_on_post' meta fields. Filter working as it should, I suppose doesn't resolve when fields empty, but here is code to make those fields not empty:
add_action('save_post', 'add_post_custom_meta');
function add_post_custom_meta() {
global $post;
$post_id = $post->ID;
update_post_meta($post_id, '_like_count_on_post_', 0);
update_post_meta($post_id, '_dislike_count_on_post_', 0);
}

wc_order_query with meta_query not working when using relation

I am trying to use WC_Order_Query, to get all orders where a custom meta_key doesn't exist, is empty or equal to 0
I've tried like a lot of the stuff documented on this site, but nothing seems to work. It just returns all content, which is the opposite of what i'm trying to do.
This is what most people have recommended so far, but it doesn't seem to work as intended or maybe I am not seeing the issue here
$args = array(
'limit' => 9999,
'return' => 'ids',
'orderby' => 'date',
'order' => 'DESC',
'status' => 'processing',
'date_created' => '>='.$startdate,
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
This ended up being the solution:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
'orderby' => 'the_date',
'order' => 'DESC',
'date_query' => array(
array(
'after' => $startdate . $starttime,
'inclusive' => true
)
),
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WP_Query( $args );
$query_posts = $query->get_posts();
Thanks a lot to Boris for the assistance.

Order wp_query by numeric custom field

I'm trying to allow users to sort a list of results from a wp_query by a numeric custom field value. Users can click a button to change the sort by values, but when Price is selected, this is what the wp_query looks like:
'meta_key' => 'adult',
'orderby' => 'meta_value_num',
'order' => 'ASC',
The field is the numeric field from ACF (ACF 5 beta is being used) with a step size of 0.01 to allow for prices like 9.99.
However, if I run this query it returns no results every time, even when there are definitely results that should be matching.
Any ideas where I'm going wrong?
EDIT - full query
$keywordString = $_SESSION['search']['keyword'];
$keywords = explode(', ', $keywordString);
$taxQuery = array(
'relation' => 'AND',
array (
'taxonomy' => 'main-cat',
'field' => 'slug',
'terms' => $_SESSION['search']['cat']
)
);
if( $_SESSION['search']['keyword'] != '' ) {
$taxQuery[] = array(
'taxonomy' => 'sub-cat',
'field' => 'name',
'terms' => $keywords
);
}
$args = array(
// general
'post__in' => $postIDs,
'post_type' => 'event',
'posts_per_page' => 10,
'paged' => $paged,
'cache_results' => false,
'meta_key' => $_SESSION['search']['sort-key'], // becomes 'adult'
'orderby' => $_SESSION['search']['sort-by'], // becomes 'meta_value_num'
'order' => 'ASC',
// category filter
'tax_query' => $taxQuery,
// date filter
meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_%_start-date',
'value' => $when,
'compare' => '>=',
'type' => 'NUMERIC'
),
array (
'key' => 'date_%_end-date',
'value' => $when2,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );

WordPress User Query with And/Or arguments

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' => '=',
)
)
);

Order by One Custom Field Value and Select Posts From Another With Wordpress

I'm trying to only show posts that are marked as in-stock and order them by their inventory_number (which is a number value so I'm using meta_value_num). The code below is selecting in-stock items, but it isn't ordering the posts by inventory_number. What am I doing wrong?
$args = array(
'numberposts' => -1,
'post_status'=>"publish",
'post_type'=>"post",
'category_name'=>"tape",
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'inventory_number',
'orderby' => 'meta_value_num',
'order' => 'asc'
),
array(
'key' => 'status',
'value' => 'in-stock',
'compare' => 'LIKE'
)
)
);
For order by on a custom field, meta_key=keyname must be present in the query. Plus I don't think you want the order by in the AND clause. So try this...
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'post_type' => 'post',
'category_name' => 'tape',
'meta_query' => array(
array(
'key' => 'status',
'value' => 'in-stock',
'compare' => 'LIKE'
)
),
'meta_key' => 'inventory_number',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);

Categories