How do I pass two meta value to WP_Query - wordpress

I'm having issues with my WP_Query. So I have two select boxes created using ACF one for Country and one for sector. When I select country and leave sector empty I get all the posts with the selected country and like wise for the sector. What I need to do is filter it further so if both are selected and not just one I get all posts from that country and sector. My args below any help is appreciated.
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'dis_country',
'value' => $country_sector,
),
array(
'key' => 'dis_sector',
'value' => $sector,
)
)
);

May be it's helpful for you -
$field_key_country = "field_57b4439b5e371";
$field_country = get_field_object($field_key_country);
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : false;
$sector_key = "field_57e15152d896d";
$sector_field = get_field_object($sector_key);
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : false;
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1
);
if($country_sector){
$args['meta_query'][] = array(
'key' => 'dis_country',
'value' => $country_sector
);
}
if($sector){
$args['meta_query'][] = array(
'key' => 'dis_sector',
'value' => $sector
);
}

Please try following code with relation AND & compare operator.
Also please check WP_Query class at https://codex.wordpress.org/Class_Reference/WP_Query
<?php
$country_sector = isset($_POST['country_select'])? sanitize_text_field($_POST['country_select']) : '';
$sector = isset($_POST['sector_select'])? sanitize_text_field($_POST['sector_select']) : '';
$args = array(
'post_type' => 'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>
'distributer_search',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dis_country',
'value' => $country_sector,
'compare' => '=',
),
array(
'key' => 'dis_sector',
'value' => $sector,
'compare' => '=',
)
)
);
?>

Related

Wordpress meta query not working with dynamic parameters

I'm trying to make a call using a custom end-point to one custom post type on my project.
I'm passing dynamic parameters to that function-
// $per_page = $_GET['per_page'];
// $page = $_GET['page'];
$per_page = 18;
$page = 1;
$type = $_GET['type'];
$order = $_GET['order'];
// $houseType = $_GET['houseType'];
$houseType = 'Terreno';
$houseState = $_GET['houseState'];
$houseArea = $_GET['houseArea'];
$housePrice = $_GET['housePrice'];
$houseRooms = $_GET['houseRooms'];
// $houseProp = $_GET['houseProp'];
$houseProp = 'venda';
$meta_query = array();
if( $houseProp ) {
$meta_query[] = array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
);
}
if ( $houseType ) {
$meta_query[] = array(
'key' => 'tipo_de_imovel',
'value' => $houseType,
'compare' => '=',
);
}
if ( $houseState ) {
$meta_query[] = array(
'key' => 'estado_do_imovel',
'value' => $houseState,
'compare' => '=',
);
}
if ( $houseArea ) {
$meta_query[] = array(
'key' => 'area_bruta',
'value' => $houseArea[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'area_bruta',
'value' => $houseArea[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $housePrice ) {
$meta_query[] = array(
'key' => 'preco',
'value' => $housePrice[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'preco',
'value' => $housePrice[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $houseRooms ) {
$meta_query[] = array(
'key' => 'quartos',
'value' => $houseRooms[0],
'type' => 'NUMERIC',
'compare' => '>'
);
$meta_query[] = array(
'key' => 'quartos',
'value' => $houseRooms[1],
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $type == "preco") {
$orderBy = array(
'meta_key' => $type,
'orderby' => 'meta_value_num',
'order' => $order,
);
} else {
$orderBy = array(
'orderby' => array(
'date' => $order,
'menu_order'=> $order,
)
);
}
$args = array_merge($orderBy, array(
'post_type' => 'imoveis',
'posts_per_page' => $per_page,
'paged' => $page,
'meta_query' => $meta_query,
));
// get posts
$posts = get_posts($args);
The problem is that the $meta_query is not working...
If I pass only 1 parameter (houseProp), I can make that work with $meta_query[0], but if I pass more parameters it does not return anything.
What am I doing wrong?
EDIT:
I was able to see that I need to update my post on the backend (by pressing the update button).
I'm creating a post like this:
$post_id = wp_insert_post(array(
'post_title' => $house->InfoGeral->SubTipoImovel,
'post_type' => 'imoveis',
'post_content' => $description[0],
'post_status' => 'publish')
);
and filling the ACF custom fields like this:
update_field('field_5d230e2057fa5', $ref, $post_id);
what could possibly be the error?
When you use $meta_query it uses the 'relation' attribute and is defaulted to 'AND'. For example, when you set it up with one parameter, like your example:
$meta_query[] = array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
);
You are really setting it to look like:
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
)
);
So now when you add a bunch of other parameters, it really looks like this:
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'tipo_de_operacao',
'value' => $houseProp,
'compare' => '=',
),
array(
'key' => 'area_bruta',
'value' => $houseArea[0],
'type' => 'NUMERIC',
'compare' => '>'
),
array(
'key' => 'area_bruta',
'value' => $houseArea[1],
'type' => 'NUMERIC',
'compare' => '<'
)
);
So my guess is you are starting to really narrow your results down to this AND that AND this other thing, which is probably too restrictive. You can change the relation attribute to 'OR', if that is your goal, or attempt to re-code it to be less restrictive.
You can fix this by changing your line of:
$meta_query = array();
to:
$meta_query = array( 'relation' => 'OR' );
and keep everything else the same.

Wordpress - WP Query : meta_query relation ignored

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

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

Wordpress get posts with multiple meta values

I have a ACF select field which takes multiple value. Now I want to use get_posts() to get those custom posts. My arguments look like this:
$party = 'test1';
$function = 'test1, test2';
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'party',
'compare' => '=',
'value' => $party,
),
array(
'key' => 'function',
'compare' => 'IN',
'value' => array($function),
)
)
);
$items = get_posts($args);
But this does not work! Don't know what is wrong here!

Resources