I am not sure if this is the correct method, but I have created a form with some options to use as triggers for filtering the WordPress loop. I do not have it set up to save any form information.
<form id="options" method="POST">
<p>Date Started</p>
<select name="date-started">
<option value="any-date">Any Date</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
</select>
</form>
I want to use the selections to filter the WordPress loop by meta data. I have a working snippet that does the filtering.
/* Filters the loop by custom meta data
https://codex.wordpress.org/Class_Reference/WP_Query#Parameters
*/
function comic_start_date( $query ) {
if ( $query->is_archive){
$query->query_vars["meta_key"] = 'date-started';
$query->query_vars["meta_value"] = '2015';
}
}
add_action( 'pre_get_posts', 'comic_start_date', 1 );
But, I do not know how to connect this to the option selected.
I have seen statements such as:
<?php if ($(("option[value='completed']")){//do something}?>
<?php if($ceg==1){//do something} ?>
<?php if($(this).value == 'volvo'){//do something} ?>
However, I have not been able to make these function together. Maybe I am using them in the wrong way.
As a bonus I would like the filter function vars to be taken from the option value and select name. I’m not sure if this is possible. I’ll settle for a working if statement.
I found the answer
This is the working if statement:
This uses the name from the select tag and the value of the option you want to target.
And this is it shown around my filter: