• itchy92

    (@itchy92)


    Hi all,
    I’m back again with another question. I’ve been working on this on and off for a couple weeks, and I still can’t get it; I was hoping someone could shed some light for me.

    I have a custom event post type, with a bunch of meta keys including:

    • _start_eventtimestamp and _end_eventtimestamp, for defining the event date(s)
    • _event_venue and _event_city, obviously for the venue and city

    On my “calendar” page which lists these events, I’ve created a filter form to search by keyword, event date, and location. Right now, I’m using query_posts, because I couldn’t get pre_get_posts to work in the functions.php file. I know it’s not ideal, but it’s mostly working.

    Right now, the code looks like this:

    $keywords = $_GET['keywords'];
    	$location = $_GET['location'];
    	$datefilter = $_GET['date'];
    
    	$args = array(
    		'post_type' => 'event',
    		'paged' => $paged,
    		'meta_key' => '_start_eventtimestamp',
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC'
    		'meta_query' => array(
    			array(
    				'key' => '_end_eventtimestamp',
    				'value' => (date('Ymdhi') - 10000),
    				'compare' => '>='
    			)
    		)
    	);
    
    	if ( $keywords ) {
    		$args['s'] = $keywords;
    	}
    
    	query_posts($args);

    This works well enough: it shows only upcoming events (via the meta query), and if I search on keyword, it returns the filtered results.

    However, my problem arises when I want to activate the “location” and “date” filters; I’d like to have a single input box that can search both _event_city and _event_venue, AND also respect the date filter if one is used.

    I’m convinced I need to switch to using a $wbdb query directly, but I don’t understand the SQL syntax / WP table structures enough to decipher the examples I’ve seen and adapt them to my needs.

    So, to recap, I need some (a lot) of help in putting together a query that would do the following:

    * Search the post title for a keyword
    AND
    * Search the _event_venue and _event_city meta keys for the same input, and return if either key’s value matches
    AND
    * Search _start_eventtimestamp and _end_eventtimestamp meta keys for a value, and return if the input falls between the two values

    I know I’m asking a lot, but I have made no progress on this, and it’s frustrating. Thank you so much for any help you can provide!

  • The topic ‘Custom Post Search Using Multiple Meta Keys’ is closed to new replies.