Wordpress filter with multiple parameters - wordpress

I'm trying to set up a wordpress page in which there is a main section with upcoming events and a secondary section in which there are passed events.
I'm using this filter (located in functions.php) in order to switch upcoming and passed events:
// upcoming events
function listing_filter_function($date_args){
$date_args = array(
'post_type' => 'evento',
'meta_key' => 'start_date',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'start_date',
'compare' => '>=',
'value' => date("Ymd"),
'type' => 'DATE'
)
),
);
return $date_args; } add_filter('listing_filter', 'listing_filter_function');
// events archive
function archive_filter_function($date_args){
$date_args = array(
'post_type' => 'evento',
'meta_key' => 'start_date',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query'=> array(
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
)
),
);
return $date_args; } add_filter('archive_filter', 'archive_filter_function');
In this filter the events are sorted using the start date, but I would also need to check the end date to prevent events lasting more than one day from being show in the archive section after the first day
Thanks in advance!

you can use the meta_query relation for this like this example
'meta_query'=> array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>='
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<'
'value' => date("Ymd"),
'type' => 'DATE'
),
)
for the archive you can check if the end_date is smaller then today or equal or less (<=)
Edit to answer the Question in the comment, this was asked after marking the answer correct. Please note I'm not 100% on this as I'm readying mixed results about this method so if it works please let us know so I can update this to reflect the results
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>'
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<'
'value' => date("Ymd"),
'type' => 'DATE'
),
),
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>'
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'value' => '1',
'compare' => 'NOT EXISTS'
),
),
),

it finally works!I've made some changes:
'meta_query'=> array('relation' => 'OR',
array('relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'value' => '',
'compare' => '='
),
),
array('relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
),
),

Related

WP several meta_query matches, and double counted

I set following meta_query.
I like to add one more condition.
My problem is that the article which matches both meta_query conditions are double counted by count(get_posts on the end of line.
Is it possible to add condition like this? If both 1bb and 1aa matches, count it as if only 1bb matches.
<?php
$args = array(
'category_name' => $cat,
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'or',
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
echo count( get_posts( $args ) );
?>
Thank you.
The following is additional code that I tried to prevent from double counting.
Its too long time to load, and can not use it.
'meta_query' => array(
'relation' => 'or',
array(
'relation' => 'AND',
array(
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1bb',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'EXISTS',
),
array(
'key' => '1bb',
'compare' => 'EXISTS',
),
),
),
),

Wordpress Query, check a value against multiple keys

My Wordpress setup has a Publication custom post type and has 10 custom fields each corresponding to an author who may have contributed to that publication. Every post can have different authors in those fields or the same authors but in a different order. I am using Wordpress's WP_Query to search for posts matching my criteria. I need to check a single author name against all the custom fields to see if it matches any one of them. I tried doing this with the below code but it produces no result. Any help would be appreciated!
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'key' => array(
'author_0_name',
'author_1_name',
'author_2_name',
'author_3_name',
'author_4_name',
'author_5_name',
'author_6_name',
'author_7_name',
'author_8_name',
'author_9_name',
),
'value' => $selected_pub_author,
'compare' => '=',
),
),
);
// The above meta-query may be modified through the use of this:
// echo "<pre>".print_r($args[meta_query][relation])."</pre>";
// The Query
$the_query = new WP_Query($args);
I have added OR relation for post author query,Try to change your argument array as follow,
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'relation' => 'OR',
array(
'key' => 'author_0_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_1_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_2_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_3_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_4_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_5_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_6_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_7_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_8_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_9_name',
'value' => $selected_pub_author,
'compare' => '=',
),
),
),
);
$the_query = new WP_Query( $args );

Query with meta key is serialized array

I've made a custom query containing a meta query but some fields are serialized arrays. Is there a way to get the value from that array? Everything I've tried so far won't work.
$query_args = array(
'post_type' => 'yacht',
'_meta_or_tax' => true,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'manufacturers',
'field' => 'id',
'terms' => $_GET['manufacturer']
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_GET['cat'],
)
),
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'yachts_length',
'value' => array($_GET['min_length'], $_GET['max_length']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_price',
'value' => array($_GET['min_price'], $_GET['max_price']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_year',
'value' => array($_GET['min_year'], $_GET['max_year']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_fuel',
'value' => $_GET['fuel'],
'compare' => 'LIKE',
),
array(
'key' => 'yachts_cabins',
'value' => $_GET['cabins'],
'compare' => 'LIKE',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_engine_type',
'value' => $_GET['engine_type'],
'compare' => 'LIKE',
),
array(
'key' => 'yachts_en',
'value' => $_GET['engines'],
'compare' => 'LIKE',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_drive',
'value' => $_GET['drive_type'],
'compare' => 'LIKE',
),
)
);
$yacht_query = new WP_Query( $query_args );
yachts_cabins, yachts_fuel, yachts_engine_type, yachts_en, yachts_type are serialized arrays.

Meta Queries - should nesting work after WP 4.1?

I have a rather huge meta_query here and it hangs at execution: browser just won't load the page, it goes on loading ...
As far as I know this should work after WP 4.1 (I have it). Also tried updating to 4.2 to no avail.
Am I doing something terribly wrong here? Thanks for any info.
$meta_query = array(
'relation' => 'AND',
// IF $level GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_level',
'value' => $level,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level2',
'value' => $level,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level3',
'value' => $level,
'compare' => 'IN'
),
),
),
// IF $year GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_year',
'value' => $year,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year2',
'value' => $year,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year3',
'value' => $year,
'compare' => 'IN'
),
),
),
);
$args = array(
'post_type' => 'course',
'posts_per_page' => -1, // get all posts
'orderby' => array('meta_value_num'=>'ASC','menu_order'=>'ASC'), // order by given meta value AND menu_order
'meta_key' => $meta_key_orderby,
'order' => 'ASC',
'meta_query' => $meta_query,
);

WP_Query meta_query date range with 2 custom fields

I have a custom post type 'events' that contains custom fields for start_date and end_date. I'm trying to make a simple list of upcoming events. If I query only events with a start_date >= today it works fine. If I add in a meta_query to say "AND" all events with an end_date <= today - it doesn't return anything.
My custom fields (start_date, end_date) are stored as unix timestamps, which is why I'm using 'NUMERIC' and 'meta_value_num'. $today is a timestamp of the current date. Here are examples of what I'm trying to do... Pulling my hair out - any help would be appreciated!
THIS WORKS:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
)
);
When I add in a second array for the meta_query - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
)
);
When I use meta_compare in the main query, and a single meta_query - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $today,
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'date',
),
)
);
When I use meta_compare in the main query, and multiple meta_queries - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $today,
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
)
);
This is how I'm using it and its working fine for me.
$the_query = new WP_Query(array(
'post_type' => 'job',
'posts_per_page' => 25,
'meta_query' => array(
array(
'key' => 'published_date',
'value' => array('20140401','20140405'),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
)
));
if ($the_query->have_posts()) {
echo '<ul>';
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
echo 'Sorry! No Posts';
}
wp_reset_postdata();
The most important note on the codex.
The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format.
Here's what I ended up doing:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $yesterday,
'meta_compare' => '<=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => array($first_minute,$last_minute),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $yesterday,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'start_date',
'value' => $yesterday,
'compare' => '>=',
'type' => 'NUMERIC',
),
)
)
This gets the current month's events. I input variables for first minute of month, last minute of month, and "yesterday" which is 11:59:59 of yesterday.
Not sure what are you trying but seems logical to not return anything as you are selecting events that starts today or in the future (start_date >= $today) AND ends today or in the past (end_date <= $today)...
Alexandru-Florentin Popescu is right! Use your code another following:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
));
You just need change key 'compare' in both arrays

Resources