I'm working on a wp-query using php.
first I need to get an array of posts ID, using this
$artists_ID_array = get_field("page_artiste");
(I'm using Advanced Custom Fields with relationship, anyway it doesn't matter... I get an array of values)
here is what the array looks like :
Array ( [0] => 141 [1] => 59 )
then comes my args :
$videos = get_posts(
array(
'post_type' => 'videos',
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[0],
'compare' => 'LIKE'
),
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[1],
'compare' => 'LIKE'
),
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[2],
'compare' => 'LIKE'
),
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[3],
'compare' => 'LIKE'
),
array(
'key' => 'page_artiste',
'value' => $artists_ID_array[4],
'compare' => 'LIKE'
and so on... in my meta query, I'm using all the arrays results.
It works fine, I'm getting the posts I want.
But as you can imagine what I'm trying to do is to use for each to avoid having all thoses arrays inside my args "$artists_ID_array[4]", and looping inside my array.
so here is what I've tried, but it's not working... and I can't understand why...
<?php
$artists_ID_array = get_field("page_artiste");
$videos = array(
'post_type' => 'videos',
'post__not_in' => array( $post->ID ),
'meta_query' => array(
)
);
foreach($artists_ID_array as $value) {
array_push($videos['meta_query'], array(
'key' => 'page_artiste',
'value' => $value,
'compare' => 'LIKE'
));
}
?>
can anybody help me with this ?
hope you understand my request
I believe you can simplify this even further, since the meta_query compare allows you to use the value "IN"
"IN" (instead of like) allows you to search the values for any of the values in a given array.
So your get_posts could look something like this:
$videos = get_posts(
array(
'post_type' => 'videos',
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'page_artiste',
'value' => $artists_ID_array,
'compare' => 'IN'
)
)
)
);
Related
I am performing a WP_Query on a custom post type called "task" that has a field "stage". I need to get all of the posts with a "stage" >= the "stage" of another task. Problem is stage is a string value, so using >= operator in the meta query won't work.
Currently my hack is to create an array that contains string value of numbers $stage to 50, and query for posts who's stage is in that array. This will work for the time being as no stage is above 12, but not a very scalable or dynamic solution. I'm wondering if there is a better way to mutate a value recieved in WP_query before the operator is used on it?
$stage = get_field('stage', $task_id);
$future_tasks = array();
for ($i = intval($stage); $i <= intval($stage) + 50; $i++) {
array_push($future_tasks, strval($i));
}
$tasks_query_args = array(
'post_type' => array('task'),
'posts_per_page' => -1,
'order' => 'DESC',
'meta_query' => array(
'0' => array(
'key' => 'project',
'value' => $project_id,
'compare' => '=',
),
'1' => array(
'key' => 'stage',
'value' => $future_tasks,
'compare' => 'IN',
),
'relation' => 'AND',
),
);
Ideally the second parameter of the query would be something like:
'1' => array(
'key' => 'stage',
'value' => intval($stage),
'compare' => '>=',
),
But that won't work because value of the stage field is a string.
Wondering if there are any ideas about how to better achieve this?
Seems like, using numeric type could solve the problem:
$tasks_query_args = array(
'post_type' => array( 'task' ),
'posts_per_page' => - 1,
'order' => 'DESC',
'meta_query' => array(
'0' => array(
'key' => 'project',
'value' => intval( $project_id ),
'compare' => '=',
'type' => 'numeric',
),
'1' => array(
'key' => 'stage',
'value' => intval( $task_id ),
'compare' => '>=',
'type' => 'numeric',
),
'relation' => 'AND',
),
);
Summary
In my Wordpress build, I have a calendar made of custom event posts. These posts have two settings, single day event, and multiple day event. This is set as a boolean. I need to grab these posts, and sort them based on their date.
Problem
The issue I'm facing is that when I query the back-end, I don't know which date to sort by. I've tried using the boolean to create a sort of "if then" logic, but no luck.
Trouble Shooting
What have I tried so far?
This is the query I've been trying to get to work. No luck though
$events_query = new WP_Query (array(
'post_type' => 'events',
'posts_per_page' => '-1',
'meta_query' => array (
'relation' => 'AND',
array (
'key' => 'multiple_day_event',
'value' => '1',
'compare' => '=',
),
array (
'key' => 'start_date',
'value' => date("Ymd"),
'compare' => '>',
),
'relation' => 'OR',
array (
'key' => 'multiple_day_event',
'value' => '0',
'compare' => '=',
),
array (
'key' => 'date',
'value' => date("Ymd"),
'compare' => '>',
)
)
));
Can I use a single date instead of two separate dates?
No, not to my knowledge. Using a single date in place of both has causing other issues in my back-end.
Solution
In short, I need a query that's able to use OR, AND, OR logic. Any ideas?
According to the WP_Meta_Query docs:
Nested arrays can be used to construct complex queries
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => '_my_custom_key',
'value' => 'Value I am looking for',
'compare' => '='
),
array(
'relation' => 'AND',
array(
'key' => '_my_custom_key_2',
'value' => 'Value I am looking for 2',
'compare' => '='
),
array(
'key' => '_my_custom_key_3',
'value' => 'Value I am looking for 3',
'compare' => '='
)
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
So your example would look something like this:
$events_query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => '-1',
'meta_query' => array (
'relation' => 'AND',
array(
'relation' => 'AND',
array (
'key' => 'multiple_day_event',
'value' => '1',
'compare' => '=',
),
array (
'key' => 'start_date',
'value' => date("Ymd"),
'compare' => '>',
),
),
array(
'relation' => 'OR',
array (
'key' => 'multiple_day_event',
'value' => '0',
'compare' => '=',
),
array (
'key' => 'date',
'value' => date("Ymd"),
'compare' => '>',
)
)
)
) );
I am working on custom search by meta value and using this:
print_r($_REQUEST);
$args = array(
'post_type' => 'property_post',
'posts_per_page' => 10,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'custom_textarea',
'value' => 'me', // if I use static keyword it works
'compare' => 'LIKE'
),
array(
'key' => 'custom_price',
'value' => array( $_REQUEST['custom_price'], $_REQUEST['custom_price1'] ),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'custom_beds',
'value' => $_REQUEST['custom_beds'],
'compare' => '='
),
array(
'key' => 'custom_bath',
'value' => $_REQUEST['custom_bath'],
'compare' => '='
),
array(
'key' => 'custom_garage',
'value' => $_REQUEST['custom_garage'],
'compare' => '='
)
)
);
If I use some static keyword for meta value then it works but with the $_REQUEST it did not.
I checked $_REQUEST by print_r($_REQUEST).
Array ( [custom_textarea] => aa[custom_price] => 1000 [custom_price1] => 4000[custom_beds] => 2[custom_garage] => 1)
So what should i do to make it fine.
Thanks in advance.........
Store the $_REQUEST values to some variables and use it in the query
$a=$_REQUEST['custom_price'];
and
'value' => $a,
Im having trouble with my WordPress query on a project I do for a customer of us. Basically my problem is that I want to grab posts from the database with specified meta_keys. I want to get my posts (wpcompare) with the meta key '_price' and value between xx and yy. Works fine so far. Now I want to add filters for manufacturer, categories and tags. These filter values are all multiple, so that you can select multiple manufacturers. For example Canon and Nikon. So with the WP_MetaQuery I can filter multiple meta_keys and values, but I cant combine the queries with AND or OR.
Let me give you an example, how my query should work:
"Give me all the posts with post_type "wpcompare" where the meta_value _price is between 100 and 200 bucks, and where the manufacturer (_hersteller) is Canon OR Nikon OR Sony".
My head turns crazy, so please help me.
Thank you very much in advance :-)
Here is my Code:
if(isset($_POST) AND !empty($_POST))
{
$meta_query = array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
);
}
else
{
$meta_query = '';
}
$args = array(
'post_type' => 'wpcompare',
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => $meta_query,
'posts_per_page' => ($per_page == false) ? 18 : $per_page,
'ignore_sticky_posts'=> true
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args)
$args = array(
'post_type' => 'posttypehere',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'somekey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'anotherkey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ($query->have_posts()) : $query->the_post();
echo $post_id = get_the_ID();
endwhile;
endif;
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'somekey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'anotherkey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
);
I've got some custom fields as meta data for a new post type - Properties (for an estate agents), so want to search by number of bedrooms, min/max value and location. I have a form with multiple drop-downs for each of these fields:
location, min_value, max_value, bedrooms
Also, I have meta boxes on the posts themselves, so one for price, bedrooms, location, and a taxonomy type of property_type - rent, sale, and commerical.
I've found this piece of code online but not sure how to manipulate it so it takes whatever value the form takes?
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'location',
'value' => '[LOCATION HERE]',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => '[PRICE HERE FROM FORM]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
Also, I understand the search query goes on function.php but do I call it from where the form is, or where the results are outputted? ie. my homepage or my searchpage?
Hope someone can help
use this code
$args = array(
'post_type' => 'Properties',
'meta_query' => array(
array(
'key' => 'location',
'value' => '[LOCATION HERE]',
'compare' => 'LIKE'
),
array(
'key' => 'min_value',
'value' => '[min value here]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
array(
'key' => 'max_value',
'value' => '[max value here]',
'type' => 'numeric',
'compare' => 'BETWEEN'
)
array(
'key' => 'bedrooms',
'value' => '[bedroom here]',
'compare' => 'LIKE'
),
)
);
$query = new WP_Query( $args );
and You have to call this in your searchpage....
Thanks for help Yogesh, I modified your answer to get this which seems to work:
<?php $args = array(
'post_type' => 'Property',
'property_type'=>$_GET['type'],
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_property_info_location',
'value' => Cuztom::uglify($_GET['location']),
),
array(
'key' => '_property_info_bedrooms',
'value' => $_GET['bedrooms'],
),
array(
'key' => '_property_info_price',
'value' => $_GET['max_value'],
'compare' => '<=',
'type' => 'numeric',
),
array(
'key' => '_property_info_price',
'value' => $_GET['min_value'],
'compare' => '>=',
'type' => 'numeric',
),
),
);
$the_query = new WP_Query( $args );
?>