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' => '=',
)
)
);
Related
I have an array:
$args = array(
'orderby' => 'meta_value',
'meta_key' => $columnName,
'order' => $columnOrder,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'shipping_company',
'value' => '^'.$prospectSearch,
'compare' => 'REGEXP',
),
array(
'key' => 'shipping_company',
'value' => '---NV',
'compare' => 'NOT LIKE',
),
array(
'key' => 'shipping_company',
'value' => '---DS',
'compare' => 'NOT LIKE',
),
array(
'key' => 'customer_status',
'value' => '1',
'compare' => '=',
),
),
);
Along with that I also need to search the user_meta table for many many zip codes in a loop like this:
$args['meta_query'][]=array(
'relation' => 'OR',
);
foreach($zips as $zip) {
$args['meta_query'][]=array(
array(
'key' => 'shipping_postcode',
'value' => $zip,
'compare' => '=',
),
);
}
The problem is that the 'OR' in the zip code portion always gets changed to an 'AND'. I feed this array to WP_User_Query like this:
$wpUserQuery = new WP_User_Query($args);
I use the following line to get the SQL:
$wpUserQuery->request;
I went a different route. Rather than using WP_User_Query I am going the build the SQL string manually and optimize it.
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.
I'm trying to order by the custom field InPrice but not able to do that. I'd know if possible in another way makes it ('orderby' => 'meta_value_num', 'order' => 'ASC',)
$args = array(
'post_type' => 'post',
'posts_per_page'=>-1,
'meta_key' => 'InPrice',
'meta_value' => array( $pieces[0], $pieces[1]),
'meta_type' => 'numeric',
'meta_compare' => 'BETWEEN',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'btown' => array(
'key' => 'Town',
'value' => $town,
'type' => 'STRING',
'compare' => 'LIKE',
),
broom' => array(
'key' => 'Rooms',
'value' => $rooms,
'compare' => 'LIKE',
),
),
),
);
referred to Wordpress Documentation
you can use something like code below :
$args = array(
'post_type' => 'post',
'posts_per_page'=>-1,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'age',
'value' => array( array( $pieces[0], $pieces[1]) ),
'compare' => 'IN',
), // other meta queries that you want
//don't forget to set a relation type for them in here ex:'relation'=>'AND',
),
);
$query = new WP_Query( $args );
I want to display popular post in custom page , I have added post views and post like option in wordpress and I want to show popular posts based views and likes,
I tried this code but it's ranking only for views counter ;
$args = array(
'posts_per_page'=>5,
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
'_post_views' => array(
'key' => '_post_views',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
'_post_like_count' => array(
'key' => '_post_like_count',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
),
);
thanks for answers
Try new orderby feature:
$args = array(
'posts_per_page' => 5,
'meta_query' => array(
'relation' => 'AND',
'_post_views' => array(
'key' => '_post_views',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
'_post_like_count' => array(
'key' => '_post_like_count',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
),
'orderby' => array(
'_post_views' => 'DESC',
'_post_like_count' => 'DESC'
)
);
I have the following function which returns only future events, which works great:
$args = array(
'post_type' => self::POST_TYPE,
'posts_per_page' => $posts_per_page,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'status' => 'publish',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
)
)
);
The problem I have is, I also need to check whether a custom field called "post_is_global" has been set (the type is BOOL by the way) but I don't know how to implement it into this query. Any help would be greatly appreciated.
Many thanks!
The query should look somewhat like this:
$args = array(
...
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'post_is_global',
'value' => '1',
'compare' => '=',
),
)
);
$query = new WP_Query($args);
References:
ACF | Query posts by custom fields
Class Reference/WP_Query