WordPress Meta Query with Multi Value Arrays - wordpress

I have the following query that I would like to pass an array of values to:
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
array(
'key' => '_customer_names',
'value' => $customer_names,
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => $customer_dates,
'compare' => 'LIKE'
)
)
)
);
For example, I would like to pass like this:
$customer_names = array('John','Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
The query will also need to handle the possibility that all customers could have data for each date. In pseudo SQL, my best guess would be:
SELECT * FROM WORDPRESS_POSTS WHERE _customer_names = (John OR Tom OR Simon) AND customer_dates = (20161225 OR 20161226 OR 20161227)
However, at the moment, even when I remove the date restriction, I can't find any posts. Hence, I wanted to confirm my logic is correct.

Try like this:
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => '_customer_names',
'value' => 'John',
'compare' => '='
),
array(
'key' => '_customer_names',
'value' => 'Tom',
'compare' => '='
),
array(
'key' => '_customer_names',
'value' => 'Simon',
'compare' => '='
)
),
array(
'relation' => 'OR',
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
),
array(
'key' => '_customer_dates',
'value' => '20161225',
'compare' => '='
)
),
)
)
);

if you want to use array in values you need to use compare with IN clause.
OR if you want to match with exact values you can try #Ravendra Patel's solution.
Also use WP_QUERY because it comes with main query and provide more help to identify the issue
$customer_names = array('John', 'Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
$args = array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
array(
'key' => '_customer_names',
'value' => $customer_names,
'compare' => 'IN'
),
array(
'key' => '_customer_dates',
'value' => $customer_dates,
'compare' => 'IN'
)
)
);
$query = new WP_QUERY($args);
echo 'SQL: '.$query->request.'<br>'; // your query against args

try like this:
$customer_names = array('John', 'Tom', 'Simon');
$customer_dates = array('20161225', '20161225', '20161225');
$cm_metaq = array();
foreach($customer_names as $cm){
$cm_metaq[] = array('key' => '_customer_names', 'value' => $cm, 'compare' => '=');
}
$cd_metaq = array();
foreach($customer_dates as $cd){
$cd_metaq[] = array('key' => '_customer_dates', 'value' =>$cd, 'compare' => '=');
}
$data = get_posts( array(
'post_type' => 'custom_type',
'post_status' => 'any',
'posts_per_page' => 200,
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
$cm_metaq
),
array(
'relation' => 'OR',
$cd_metaq
),
)
)
);

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.

ACF Meta_Query and Get_posts is not working

I am trying to query custom post type but I keep getting no result. Is my meta_query the culprit? What is wrong with this code?
I am trying to spot the issues but i cannot find anything.
$catname = 'travel';
$priority ='high';
$status = 'incomplete';
$args = array(
'post_type' => 'my_gallery_post',
'orderby' => 'id',
//'fields' => 'ids',
'sort_order' => 'asc',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'category_it_belongs',
'value' => $catname,
'compare' => '='
),
array(
'key' => 'levelof_priority',
'value' => $priority,
'compare' => '='
),
array(
'key' => 'progress',
'value' => $status,
'compare' => '='
),
)
);
$mypost = get_posts($args);
print_r($mypost);
Output
Array()
I solved the problem by using array for 'levelof_priority'.
The field type for 'levelof_priority' is radio button whereas the other 2 are text field.
Below are my final code that's working
$catname = 'travel';
$priority ='high';
$status = 'incomplete';
$args = array(
'post_type' => 'my_gallery_post',
'orderby' => 'id',
//'fields' => 'ids',
'sort_order' => 'asc',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'category_it_belongs',
'value' => $catname,
'compare' => '='
),
array(
'key' => 'levelof_priority',
'value' => array($priority),
'compare' => '='
),
array(
'key' => 'progress',
'value' => $status,
'compare' => '='
),
)
);
$mypost = get_posts($args);
print_r($mypost);

Wp_Query meta_query not working

I'm trying to do the equivalent of this in meta_query but so far no success.
WHERE tm_url_1="" AND tm_url_2="" AND (tm_url_3="" OR tm_embed_code=!="") AND (tm_url_3!="" OR tm_embed_code=="")
Here the WP_Query. Once i add the OR clause nothing is returned.
$args = array(
'post_status' => $post_status,
'posts_per_page' => $posts_count,
'ignore_sticky_posts' => 1,
'post_type' => $post_type,
'order_by' => $order_by,
'order' => $order,
"meta_query" => array(
array('relation' => 'AND',
array(
'key' => 'tm_url_1',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_url_2',
'value' => '',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '='
)
)
)
)
);
Nothing is returned. Where did i go wrong in the meta_query?
you should remove the first "array" in the media_query parameter:
"meta_query" =>
array(
'relation' => 'AND',
array(
'key' => 'tm_url_1',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_url_2',
'value' => '',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '='
)
)
)
see: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Here is the correct way to add the meta_query for the conditions you want.
$args = array(
'post_status' => $post_status,
'posts_per_page' => $posts_count,
'ignore_sticky_posts' => 1,
'post_type' => $post_type,
'order_by' => $order_by,
'order' => $order,
'meta_query' => array(
array(
'key' => 'tm_url_1',
'value' => '',
) ,
array(
'key' => 'tm_url_2',
'value' => '',
) ,
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
) ,
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
) ,
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
) ,
array(
'key' => 'tm_embed_code',
'value' => '',
)
)
)
);
Note that I have removed the default values for compare in some arrays assuming it is = as in your condition. The reference for this can be seen here.

get post base on latest date without comparision

I have custom post type having meta value include last update date.
I have to get post id in which meta value having latest date without compare other date.
below is my wp query to get post id but it is not working.
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'meta_value_num' => 'ASC','ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
If you are using WP 4.2+, you may try the following:
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'post_last_update_date' => 'ASC', 'ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
Some sources:
General WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
4.0+ orderby array: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/
4.2+ orderby meta data: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

Wordpress meta_query to compare 3 meta value with same meta key

I would like to fetch all post that have meta_value Book, Study, Art. But when I put 3 array on meta_query system doesn't return anything, but if I put online 2 array It's work
// this working
$args = array(
'numberposts' => 10,
'post_type' => 'content',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'media_type',
'value' => 'Book',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Study',
'compare' => 'LIKE'
)
)
);
//this now working
$args = array(
'numberposts' => 10,
'post_type' => 'content',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'media_type',
'value' => 'Book',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Study',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Art',
'compare' => 'LIKE'
)
)
);
It sounds like you are searching for the exact meta values, so did you try:
$args = array(
'posts_per_page' => 10,
'post_type' => 'content',
'meta_query' => array(
array(
'key' => 'media_type',
'value' => array( 'Art', 'Book','Study' ),
'compare' => 'IN'
),
),
);
as your query arguments?
Sidenote: The parameter numberposts works, but posts_per_page is now more commonly used in WP_Query().

Resources