Reputation: 29
I'm struggling to work out how to pass the output of a custom search form in Wordpress to the search results page I have created, so that I can search by the specific post meta.
I have created a custom post type ("journeys") and I want the user to be able to search by meta fields including date, destination, number of seats etc. (The form code I am using is at the bottom of this question).
This form passes the values for each input field to the search results page via the URL, e.g.:
(website)/?post_type=journey&destination=[destination post id]&date=2018-06-19&seats=1&starting_location=&s=
But I am not sure how I can retrieve these values from the URL in order to create the args for a wp_query so that I can filter the posts correctly.
Any help in cracking this is appreciated, I've been trying to figure this out for quite a while now. A wp_query seems the best way to go in order to sidestep Wordpress's limited search function; my sticking point is retrieving those search input values for the wp_query filter. x
The search form code is pretty standard with the addition of inputs to search for the post custom meta, but I've put it below.
<form class="search-rides" role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="hidden" name="post_type" value="journey" />
<label>Select Your Destination</label>
<select id="select_destination" name="destination" class="destination" required>
<option value="0">- Choose a Destination -</option>
<?php global $post; $args1 = array( 'numberposts' => -1,
'post_type' => 'destination', 'orderby' => 'title', 'order' => 'ASC',
'meta_query' => array( array( 'key' => 'kloc_suspend', 'value' => 'yes', 'compare' => '!=' ), ), ); ?>
<?php $posts1 = get_posts($args1); foreach( $posts1 as $post ) : setup_postdata($post); ?>
<option value="<?php echo $post->ID; ?>" <?php selected($destination_post_id, $post->ID); ?>>
<?php $address4 = get_post_meta(get_the_ID(), 'address4', true); ?>
<?php the_title(); if (!empty ($address4)) { echo ' - '; echo $address4; } ?>
</option>
<?php endforeach; $post = $post_old; setup_postdata( $post ); wp_reset_postdata(); wp_reset_query(); ?>
</select>
When do you want to travel?
<input id="datepicker" name="date" class="date" type="date" required />
How many seats do you need?
<input id="number-seats" name="seats" class="number-seats" type="number" value="1" min="1" max="10" required />
<label>Search for keywords</label>
<input class="search" type="text" name="s" placeholder="Search Journeys"/>
<input class="button filled" type="submit" alt="Search" value="Search Rides" />
</form>
This is how I'm referencing the custom search results page:
function template_chooser($template)
{
global $wp_query;
$post_type = get_query_var('post_type');
if( $wp_query->is_search && $post_type == 'journey' )
{
return dirname( __FILE__ ) . '/assets/templates/search-journeys.php';
}
return $template;
}
add_filter('template_include', 'template_chooser');
** Update **
So thanks to Git-E-Up for a prompt answer on how to review those query values from the search form via the URL. But I'm still not getting my head around this properly as I'm getting zero results/404 depending on what meta I search by. Any help much appreciated, here's some extra info:
The meta fields being compared for the query filter are:
The search form is still as laid out in the question above.
This is how I've set up the query args on the results page (each of the $_GET variable names match the names of the input fields on the search form):
<?php
$seats = $_GET['seats'];
$destination = $_GET['destination'];
$journey_date = $_GET['date'];
$args = array(
'post_type' => 'journey',
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'number-seats',
'value' => $seats,
'compare' => 'LIKE',
),
array(
'key' => 'destination_post_id',
'value' => $destination,
'compare' => 'LIKE'
),
array(
'key' => 'travel_date',
'value' => $journey_date,
'compare' => 'LIKE'
),
),
);
$the_query = new WP_Query( $args ); (etc)
** Update **
If I hardcode the values into the args, then the filtering works and gives me the filtered results. But if I utilise the dynamic implementation, when I complete the contact form and submit, it gives me a 404.
If I leave the form blank and submit, I am taken to the results page and var dumps all work for each of the $_GET variables. But obviously no results show as there are no actual search criteria.
So when I submit a blank form I get this URL:
/?post_type=journey&destination=0&date=2018-06-19&seats=1&starting_location=&s=
and the var_dumps give:
seats = 1 (default is 1), destination = 0 (as none can be selected), date = (today's date, default)
If I manually amend the URL with the values I want, it also takes me to a 404 page. Very odd.
update
I've tried flushing the permalinks.
Upvotes: 0
Views: 3206
Reputation: 29
This is how I did it. Huge thanks to Git-E-UP for the help!
I've simplified my set-up for the answer so it's more universal.
Search form:
<form class="search-rides" role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="hidden" name="post_type" value="YOUR_POST_TYPE" />
<label>Date</label>
<input name="DATE_INPUT_NAME" type="date" required />
<label>Number</label>
<input name="NUMBER_INPUT_NAME" type="number" min="1" required />
<label>Search for keywords</label>
<input name="s" type="text" placeholder="Keywords"/>
<input class="button filled" type="submit" alt="Search" value="Search" />
</form>
Search results page:
<?php
// $_GET values - these retrieve the values being sent via the URL, we assign them to strings
$number = $_GET['NUMBER_INPUT_NAME'];
$date = $_GET['DATE_INPUT_NAME'];
// wp_query - use the strings in the wp_query
$args = array(
'post_type' => 'YOUR_POST_TYPE',
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'NUMBER_META_NAME',
'value' => $number,
'compare' => 'LIKE',
),
array(
'key' => 'DATE_META_NAME',
'value' => $date,
'compare' => 'LIKE'
),
),
'orderby' => array(
'DATE_META_NAME' => 'ASC',
),
);
$results = new WP_Query( $args ); if ( $results->have_posts() ) :
$results_count = $results->found_posts; echo $results_count; if ($results_count > 1) { echo ' Results';} else { echo ' Result';}
while ( $results->have_posts() ) : $results->the_post();
[YOUR RESULTS]
endwhile; endif;
?>
Upvotes: 1