Wp_query with 2 meta keys and array of meta values - wordpress

Hi in my post_type=shop i have 2 meta keys and array of values
Custom fields
Name Values
cu_status pending,processing,completed
cu_date 12-Jan-2016 , 13-Jan-2016, ...... any date in the same format date("d-M-Y")
Now i need to loop through all posts with cu_status =pending,processing and cu_date is between 12-Jan-2016 to 13-Apr-2016
What will the query ?
Iam very confused . For to get all post with status pending,processing I know the query
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
)
),
'posts_per_page' => -1
);
Pleases help to complete the query .

You need to use the relation operator e.g.
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
) ,
array(
'key' => 'cu_date',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
),
'posts_per_page' => -1
);
Also use the compare=>BETWEEN for getting the difference in 2 dates.
You may need to tweak the code a bit as I have not tested it.

Related

Sort WP_Query using multiple meta_keys not working

I have a custom post type, that has price in 2 different meta keys, based on product type. I want to filter the query so that all products can be displayed in either asc or desc order. Here is my query and it is not sorting the results.
$args = array(
'post_type' => 'ars-products',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'ars_product_price',
'compare' => 'EXISTS',
),
array(
'key' => 'ars_product_min_amount',
'compare' => 'EXISTS',
)
),
array(
'relation' => 'OR',
array(
'key' => 'ars_product_type',
'value' => 'one_time',
'compare' => '=',
),
array(
'key' => 'ars_product_type',
'value' => 'donation',
'compare' => '=',
),
),
),
'orderby' => array(
'ars_product_price' => 'ASC',
'ars_product_min_amount' => 'ASC'
)
);
$products_data = new \WP_Query( $filters );
Edit: it does sort perfectly, if I only use one meta key ars_product_price.

Custom category and meta key search in wordpress

I am trying to fetch the result if keyword, category and meta value is not empty. I mean to say that if the keyword is test and city is Mumbai and category is pet then show existing results that come in these parameters. Now I am getting all the results which have in other categories too.I have two inputs , one for keyword, second for City, zip code and third one for categories drop down.
Any suggestions would be greatly appreciated.
Expected result should be keyword,city under selected category.
$arg = array(
'post_type' => 'post',
'posts_per_page' => 10,
's' => $keyword,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' =>$cats
)
),
'meta_query' => array(
array( 'key' => 'city', 'value' => $query_city, 'compare' => 'LIKE' ),
array( 'key' => 'country', 'value' => $query_city, 'compare' => 'LIKE' ),
array( 'key' => 'postalcode', 'value' => $query_city, 'compare' => 'LIKE' ),
'relation' => 'OR'),
);
$query = new WP_Query( $arg );
If I understand you right, using 'relation' => 'AND' instead 'relation' => 'OR' should solve your issue.

How to combine ordering on multiarray meta_key query

I have a query, where I'm first checking for all posts which have meta_key topbox (kind of featured post). After that I'm checking, what custom order they have set in meta_key topbopx_order.
So far it works fine, unless two posts have the same order value, then it starts behave unpredictably. It seems to me, that it doesn't take date into account at all, because I have many more posts in the row, which have newer publish date and order set to 1, but they don't display.
This is my query so far
$feat_args = array(
'posts_per_page' => 5,
'post_status' => 'publish',
'meta_key' => 'topbox_order',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'asb_topbox',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'topbox_order',
'value' => array(1, 2, 3, 4, 5),
'compare' => 'IN'
)
),
'orderby' => array (
'meta_value_num' => 'ASC',
'date' => 'DESC'
),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
);
$feat = new WP_Query( $feat_args );
Plus I'm setting topbox_order default for all posts to value 1.
Edit
I see now, that I'm maybe trying to achieve something impossible. I'd like to set the order to posts, but at the same time, I like them to be pushed down by newer posts with same or higher order number. Is there a combination of compare that would allow this behavior?
The solution was way over my head and seemed too complicated. After some talk with client we decided to go a different route - to control only the first post in the row.
Here is what I ended up with - I'm calling two sets of posts, order them before they get to the main wp_query and just display.
// first query
$first_ids = get_posts( array(
'fields' => 'ids',
'posts_per_page' => '1',
'post_status' => 'publish',
'orderby' => 'date',
'meta_key' => 'asb_topbox',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'asb_topbox',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'topbox_ofset',
'value' => 'no',
'compare' => '='
)
)
));
// second query
$second_ids = get_posts( array(
'fields' => 'ids',
'posts_per_page' => '5', // because one can be the same as first
'post_status' => 'publish',
'orderby' => 'date',
'meta_key' => 'asb_topbox',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'asb_topbox',
'value' => 'yes',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'topbox_ofset',
'compare' => 'EXISTS'
),
array(
'key' => 'topbox_ofset',
'compare' => 'NOT EXISTS'
)
)
)
));
// Remove duplicates
$mergedposts = array_unique( array_merge( $first_ids, $second_ids));
$feat_args = array(
'posts_per_page' => 5,
'post_status' => 'publish',
'post__in' => $mergedposts,
'orderby' => 'post__in',
'ignore_sticky_posts' => true,
'no_found_rows' => true
);
In principle, you can set the post to skip to second position. And no, I didn't wanted to use sticky for this, as with time I would have sticky posts all over the place and didn't wanted to keep in mind, that I have to make them unstick. My solution keeps the date flow and I can hold the first posts for some longer time without affecting the rest of the archives.

wp_query with a variable quantity of arguments

I want to build a dynamic set of arguments for a wp_query but have had real problems doing this. Take the following example code (which does work)...
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Primary',
'compare' => 'LIKE'
),
array(
'key' => 'years',
'value' => 'Secondary',
'compare' => 'LIKE'
)
)
);
// get results
$the_query = new WP_Query( $args );
The idea is that depending on the results of user input in a form, the number of clauses in my meta_query value would alter. In the code above there are two options, but maybe depending on input there are 3 or another time 5.
I tried compiling these inner array elements externally of $args. Assume in following code that $inputArray is a single dimensional array of string elements. The test looked like:
$inputArray=array();
if (is_array($yearsArray)){
foreach( $yearsArray as $year ) {
$inputArray[]=array('key' => 'years','value' => $year,'compare' => 'LIKE');
}
}
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
'relation' => 'OR',
$inputArray
)
);
But the query just runs as if no meta_query queries had been applied.
I also tried using the function wp_parse_args to try and merge multiple meta_queries, however it seems that I'm overwriting the value and only the last one is ever used. This looked like this:
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills'
);
$args2 = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Secondary',
'compare' => 'LIKE'
)
)
);
$args3 = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Primary',
'compare' => 'LIKE'
)
)
);
$args=wp_parse_args($args,$args2,$args3);
So as you can see I've tried a few different methods but none are working. Can anyone assist?
I fixed this by discovering that you can use IN as a compare clause. $yearsArray was just a single dimensional array of string values.
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
array(
'key' => 'years',
'value' => $yearsArray,
'compare' => 'IN'
)
)
);

wp_query on 2 post types, one having custom field for further filtering

This is my first question on here; I have hunted around for an answer but if it is out there already apologies.
I am using wp_query() to fetch both 'posts' and a custom post type called 'reviews'. However the custom post type has a sub type and I need to filter out all but one of these sub types just for reviews in the same query!
This is where I got to:
$type = array( 'post', 'review' );
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array( // I need to filter the review post type further by subtype
array(
'key' => 'subtype',
'value' => 'review'
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);
Obviously, when I run this the default post type is not listed as it doesnt have the subtype custom field. How can I only apply this filter (meta_query) to the custom post type in this query and not have it apply to the default post type 'post'?
Thanks in advanced,
Kris...
UPDATE:
OK, I have tried this but with no success - any ideas?
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'subtype',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'subtype',
'value' => 'review',
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);
This will check the 'subtype' only if it exists in the post (not tested)
$type = array( 'post', 'review' );
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => AND,
array(
'key' => 'subtype',
'compare' => 'EXISTS'
),
array(
'key' => 'subtype',
'value' => 'review'
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);

Resources