Using option value to filter wordpress loop

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.

Read More
/* 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.

Related posts

1 comment

  1. 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.

    if ($_POST["date-started"] === '2008') {}
    

    And this is it shown around my filter:

    function date_started_2008( $query ) {
    if ( $query->is_archive){
      $query->query_vars["meta_key"] = 'date_started';
      $query->query_vars["meta_value"] = '2008';
    }
    }
    if ($_POST["date-started"] === '2008') {
    add_action( 'pre_get_posts', 'date_started_2008', 1 ); }
    

Comments are closed.