I’m trying to set up a dropdown menu that would allow a user to select through three / four different queries, then when they select one, the current page they’re on changes depending on whatever query they clicked. Right now I have a basic html select dropdown that changes on click and I also have my PHP query code set up.. I’m not sure how to link the two so it would get whatever page your on and then show you the corresponding posts for whatever query you select.. But nonetheless, here is what I got so far.
HTML:
<form method="get" name="QueryChange">
<select name="query" onChange="this.form.submit()">
<option value="0">Select Query</option>
<option value="1">Query 1</option>
<option value="2">Query 2</option>
</select>
</form>
PHP:
<?php
$query = '';
switch( $_POST['query'] ) {
case '2':
$query = 'gdsr_sort=rating2'; // your second query
break;
case '1':
$query = 'gdsr_sort=rating'; // your first query
break;
default:
case '0':
$query = '';
break;
}
// The Query
query_posts( $query );
// The Loop
while ( have_posts() ) : the_post();
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
Right now, when you select an option via the drop down menu, it refreshes the page but does not successfully query the posts. If I set the default case ‘0’ to 'gdsr-Sort=rating' then by default posts are successfully queried. But again the dropdown menu doesn’t actually query any posts. So how would I make this dropdown successfully query posts for the current page / category a user is on?
You’re submitting the form via GET (
<form method="get">) but you’re checking$_POST['query']in your script. Try checking$_GET['query']instead.