Forums › Forums › Search & Filter Pro › Complicated custom meta field search (dates filtering)
- This topic has 1 reply, 2 voices, and was last updated 9 years, 9 months ago by Ross.
-
Anonymous(Private) February 9, 2015 at 4:50 am #11657
Hello
I have several custom meta fields where dates for custom post type ‘event’ are stored. Number of the fields varies for every post.
For proper understanding here is a piece of code which retrieves all available dates (including past dates) from meta fields and stores them in $eventDates array. Ongoing dates (greater or equal today) are stored in $ongoingDates array.
My question is: how can I make a filtering using $eventDates and $ongoingDates array data?
The code:
<?php $today = date( 'Ymd' ); // Loop stars here if ( 'event' == get_post_type() ) { // Every 'event' custom post type has a main date stored in 'event_date' meta key $eventDate = get_post_meta($post->ID, 'event_date', true); // There could be additional dates for 'event' // Сhecking custom field 'additional_dates_num' which indicates how many additional dates are present for 'event' // Possible values of 'additional_dates_num' custom field are 0 to 3 $additionalDateNum = get_post_meta($post->ID, 'additional_dates_num', true); // Checking if there are some additional dates for the event if ( $additionalDateNum > 0 ) { // Making the main date from 'event_date' custom field part of all dates array $eventDates[] = $eventDate; // Retrieving all other additional dates from 'additional_event_date_1', 'additional_event_date_2' and 'additional_event_date_3' custom fields. // These fields are activated in post editing mode depending on 'additional_dates_num' value for ($i=1; $i<=$additionalDateNum; $i++) { // Adding retrieved additional dates to the same array where main date was added before $eventDates[] = get_post_meta($post->ID, 'additional_event_date_' . $i, true); } // Sorting the date array acsending in case the dates were not in proper order sort($eventDates); // Comparing dates to today's date and writing the list of ongoing dates to $ongoingDates array foreach ($eventDates as $date) { if ($date >= $today) $ongoingDates[] = $date; } } else { if ($eventDate >= $today) { // Addind the main date to $ongoingDates and $eventDates arrays in case it is bigger or equals $today and there are no additional dates present $ongoingDates[] = $eventDate; $eventDates[] = $eventDate; } else { // Addind the main date to $eventDates array in case it is less than $today and there are no additional dates present $eventDates[] = $eventDate; } } } // Loop ends here ?>
Ross Moderator(Private) February 11, 2015 at 11:50 pm #11792Hey Michael
Adding custom values for the search is not officially supported, however you should be able to hook into the query and modify it using pre_get_posts
This post has en example of how to modify the query to show only posts from the current user:
function modify_search_filter_query( $query ) { global $sf_form_data; global $wp_query; if ( $sf_form_data->is_valid_form() && $query->is_main_query() && !is_admin()) { //we can add our modifications here: $authorID = 1; $query->set('author', implode(",", $authorID)); //not necessary but we can match the search form ID in case we are using multiple search forms /*if($sf_form_data->form_id()==797) { }*/ } } add_action( 'pre_get_posts', 'modify_search_filter_query', 21 );
So you could add your conditions in there – but you must be comfortable with using WP hooks and modifying the query.
Thanks
-
AuthorPosts