Wordpress - WP Query : meta_query relation ignored - wordpress

I need help with a WP_Query. I use the meta query argument using the OR and AND relations, but this argument seems to be ignored in the result of the query.
Here is my code :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'author' => $contact_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $user_id )
),
array(
'relation' => 'AND',
array( 'author' => $user_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $contact_id )
)
)
);
$query = new \WP_Query( $args );
$response = $query->posts;
I already tried to add this argument like suggested in here:
'suppress_filters' => false,
'is_your_custom_query' => true, // your custom flag
Even if I remplace the value of $user_id and $contact_id directly in the query with any number, the query still return the same result. I don't understand why it's not working.

Thank you for your help !
As suggested by dafoxuk, I have to remplace meta_key by key and meta_value by value. I also had to add 'compare' => 'LIKE'.
But event in this case it wasn't working. I also had to stock the author_id in a post_meta, and change the array( 'author'=> $contact_id ) condition by :
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
So the final $args array look like this :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $user_id, 'compare' => 'LIKE '
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $contact_id, 'compare' => 'LIKE' )
)
);

Related

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

Wordpress meta_query not returning results

I want a query that returns CPT posts that contains the user's input (which is $text).
Inserting meta_key and meta_value works well but putting it in a meta_query array doesn't return anything. I want to be able to search in multiple custom fields. It's a theme I made from scratch so there is no plugins and the functions.php file is pretty small so I don't think it could be a conflict.
Code for meta_key and meta_value in the query declaration (working):
$searchquery = new WP_Query(
array( 'post_type' => 'offre',
'meta_key' => 'offre_employeur',
'meta_value' => $text,
'meta_compare'=> 'LIKE' )
);
Code for meta_value array (not working):
$meta_query_args = array(
'relation' => 'OR',
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
), array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
);
$searchquery = new WP_Query(array('post_type' => 'offre'));
$searchquery->set('meta_query', $meta_query_args);
Code for the second way I tried but still no results (not working)
$args = array(
'post_type' => 'offre',
'posts_per_page' => -1,
's' => $text,
'meta_query' => array(
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
)
);
$searchquery = new WP_Query($args);
Thank you in advance for your time.
Per the docs here: https://codex.wordpress.org/Class_Reference/WP_Query
I would assert you want to scroll down to the section titled "Multiple Custom Field Handling:", which has this example, which most closely matches your situation:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
Which, taken what you've provided in your question, I would modify as follows to get the results you are after:
$args = array(
'post_type' => 'offre',
'posts_per_page' => -1,
// remove the "search" query, which restricts the results to those with titles / content that match the $text content
// 's' => $text,
'meta_query' => array(
// add the "relation" argument, default is AND, you need OR
'relation' => 'OR',
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
)
);
$searchquery = new WP_Query($args);

WordPress Meta Query with Multi Value Arrays

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

Dynamic meta_query

I'm breaking my head over here. Through a post on this website I've managed to create a custom taxonomy archive Page in WordPress. Now I'm trying to add dynamic checkbox filters to it, but I can't seem to get the meta_query working.
This line of code works like I would like it to work;
$query = array(
'post_type' => 'company',
'posts_per_page' => 999,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
array (
'key' => 'company_method',
'value' => 'Online',
'compare' => 'LIKE',
)
)
);
How ever, this one won't:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
);
$al_tax_post_qry = new WP_Query($query);
$meta_query = $al_tax_post_qry->get('meta_query');
$name = 'company_method';
$value = explode(',', $_GET[ $name ]);
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'LIKE',
);
$al_tax_post_qry->set('meta_query', $meta_query);
Whatever I enter in the URL, it keeps finding all the results and it won't filter like the first. A print_r($meta_query); gives me:
Array ( [0] => Array ( [key] => company_method [value] => Array ( [0] => Online ) [compare] => LIKE ) )
Edit 07-06-2016 // 09:00
After reading the comment stating that I should use 'IN', I've experimented a little further and when I use it withing the query itself, it gives me no results at all. It seems 'IN' is the issue.
The field I'm querying is an 'Advanced Custom Fields' field so that might have something to do with it? However the examples on their website also use the same method.
Not Working:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'company_method',
'value' => array('online', 'orange', 'apple'),
'compare' => 'IN',
)
),
);
Working:
$query = array(
'post_type' => 'company',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'company_category',
'field' => 'slug',
'terms' => $al_cat_slug
)
),
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'company_method',
'value' => 'online',
'compare' => 'LIKE',
)
),
);
Optionally I could create an array entry in the meta_query for every value in the array, but that might not be ideal.
If you define your $meta_query value as an array, you should change your compare operator. IN and NOT IN are the array specific ones.
Try this:
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
Hope it helps!
With help from this post, the issue is solved! Values are stored in a serialized way, so need a little different approach.
https://wordpress.stackexchange.com/questions/183182/meta-query-compare-in-not-working

How to use custom fields in wp_query for ordering and filtering the result at the same time?

I want to query the posts that have been set as "featured" and order them by their "priority" field at the same time.
featured field is a true/false type and priority field is a number, and they are created by ACF plugin.
here is my code, but it's not working...
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num date',
'meta_key' => 'priority',
'meta_query' => array(
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
try this :
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num',
'order'=>'DESC',
'meta_key' => 'priority',
'meta_query' => array(
relation=>'AND',
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
array(
'key' => 'priority',
'value' => array(1,6), //YOUR VALUES
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
),
);
$query = new WP_Query( $args );

Resources