Support » Fixing WordPress » Search: Different search result *URL* for post type?

  • Resolved itchy92


    Hi all,
    I’ve been trying to figure this out for almost a week now and have made virtually no progress. I’ve seen some similar questions, but nothing that really answers this specifically.

    On my site, I have a universal search bar that searches across the whole site, and returns the results in a custom search.php, with a URL of “”. This works fine.

    What I’m trying to do is this: I have a page using a different template at “”. This lists a custom post type of “event” for all upcoming events. I would like to create some “filters” for the events, such as keyword search, date range, etc. and have it return the results on the same “” URL and filter, with I guess the querystring appended.

    I have taken a few different approaches to this: first, I tried having the “Calendar” search form post to “” using some custom query variables, and tried to read the query_vars there and modify my loop. I jumped through some hoops trying to get the custom query vars to pass through (without WP dropping them), but ultimately it didn’t work; hat was about a week ago, and I honestly don’t remember the exact problem.

    Then I tried posting to “” and just using the hidden “post_type” input to specify “event” objects and the “s” variable for the keyword search. This gave me problems with the URL rewrite function I was using to get the clean “”, since it seemed to drop the “post_type” variable before it reached the loop. The rewrite code is:

    //Makes the search results show under a "Search" slug
    	function search_url_rewrite_rule() {
    		if ( is_search() && !empty($_GET['s']) ) {
    			wp_redirect( home_url("/search/") . urlencode( get_query_var('s') ) );
    	add_action('template_redirect', 'search_url_rewrite_rule');

    I tried editing this to include change the “/search” slug to “/calendar”, and also tried adding a function above it to use the “/calendar” slug. These worked insofar as they changed the URL, but then I get a 404 when I land on “”, and it also doesn’t seem to support the robustness of having multiple filters.

    If I removed the URL rewrite completely, I got it to where it would return “”, which returned the results I wanted, and I could just format the loop based on the post type. But I’d like to have my cake and eat it, too; ideally, I would like:

    1. The universal search to return “” using the “search.php” file
    2. The calendar filter to return (for example): “” using the “template-calendar.php” template

    Is this possible within the WP framework? If so, can you please provide a “For Idiots” explanation on how to achieve it? Thanks so much for any help or guidance you can provide!

Viewing 1 replies (of 1 total)
  • I got this figured out about five minutes after I posted this… maybe the act of writing it out gave me some clarity. Anyway, here’s a quick overview of the solution, in case it can help anyone in the future.

    • The URL Rewrite function was not interfering; I left it as is, so searches using the universal search bar submit to the root (index.php) and get re-written as “”, and they display correctly using the customized search.php template I’ve specified.
    • The custom “event” post filters post to “”. In the “template-calendar.php” page, I used the following to parse the values and modify the query:
    $keywords = $_GET['keywords'];
    		$location = $_GET['location'];
    		$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;
    		if ( $location ) {
    			array_push( $args['meta_query'], array( 'key' => '_event_City', 'value' => $_GET['location'], 'compare' => 'LIKE' ) );

    It’s not the most efficient way, since it discards the original query and re-submits it, but I couldn’t get it to work as a pre_get_posts filter in the functions.php and this site isn’t huge.

    Now, the only thing I’m struggling with is: I need the $location parameter to match either of two fields. That is, the custom post type has “_event_Venue” and “_event_City” custom fields, and I’d like to match the $location value to either of them. I don’t know how to do that without running the query against one, and then against the other if there are no results, but that seems really messy.

    Oh well. Hope this helps someone, and good luck!

Viewing 1 replies (of 1 total)
  • The topic ‘Search: Different search result *URL* for post type?’ is closed to new replies.