Complicated custom meta field search (dates filtering)

Forums › Forums › Search & Filter Pro › Complicated custom meta field search (dates filtering)

Viewing 2 posts – 1 through 2 (of 2 total)
  • Anonymous
    #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
    #11792

    Hey 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:

    https://support.searchandfilter.com/forums/topic/show-only-results-for-author-of-shortcoded-page/#post-9952

    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

Viewing 2 posts – 1 through 2 (of 2 total)

Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.