Support

Account

Home Forums General Issues How to filter query posts by date, when user input is not in yymmdd format?

Unread

How to filter query posts by date, when user input is not in yymmdd format?

  • Problem Summary: I have a select form that allows the end-user to search for custom posts by year and subject. The select values are by year, (example, 2014,2013,etc), but the custom posts data is stored as yymmdd, in accordance with the date picker (ex. 20140102). I want the search results page to filter the custom post types according to the dates selected. I want user input matched against ALL custom posts.

    This is my search form:

    <form role="search" class="wcmc-form" action="<?php echo site_url('/sr'); ?>" method="GET" name="yearsubject">
    
    		<select id="yearselect" class="yearselect" name="yr">
    			<option value="2014" name="yr">2014</option>
    			<option value="2013" name="yr">2013</option>
    			<option value="2012" name="yr">2012</option>
    			<option value="2011" name="yr">2011</option>
    			<option value="2010" name="yr">2010</option>
    			<option value="2009" name="yr">2009</option>
    			<option value="2008" name="yr">2008</option>
    			<option value="2007" name="yr">2007</option>
    			<option value="2006" name="yr">2006</option>
    			<option value="2005" name="yr">2005</option>
    			<option value="20%%" name="yr">2004</option>
    		</select>
    
    		<select name="subject">
    			<option value="nutrient management" name="subject">Nutrient Management</option>
    			<option value="insects and disease" name="subject">Insects and Disease</option>
    			<option value="weed management" name="subject">Weed Management</option>
    			<option value="vegetable management" name="subject">Vegetable Management</option>
    			<option value="forages" name="subject">Forages</option>
    			<option value="grain topics" name="subject">Grain Topics</option>
    			<option value="soil, water and climate" name="subject">Soil, Water and Climate</option>
    			<option value="manure" name="subject">Manure</option>
    			<option value="agriculture business" name="subject">Ag Business</option>
    			<option value="agriculture regulation" name="subject">Ag Regulation</option>
    			<option value="genetics" name="subject">Genetics</option>
    			<option value="all subjects" name="subject">ALL SUBJECTS</option>
    		</select>
    
    		<input type="submit" name="submit" alt="Search" value="Go" />
    			
    	</form>

    As you can see from above, the form’s action takes us to page-sr.php:

    get_header(); 
    //maps the raw $_GET data to array
    $gets =array("year"=> $_GET["yr"],"subject"=> $_GET["subject"], "author"=>$_GET["auth"], "keyword"=> $_GET["keyword"]);
    
    //creates an array, for later use.
    $arr=array();
    
    // A sort of lookup table for dates
    
    //accepts $gets array filters out unset values, updates $arr
    foreach ($gets as $key=>$val){
    	if(isset($val)){
    		$arr[$key]=$val;
    	}
    }
    
    	switch($arr){
    		case (isset($arr["year"]) && isset($arr["subject"])):
    				//print_r("year and author");
    				$year=intval($arr["year"]);
    				$subj=$arr["subject"];
    
    					$args = array(
    			'numberposts' => -1,
    			'post_type' => 'wcmc',
    			'meta_query' => array(
    									'relation' => 'AND',
    											array(
    												'key' => 'date', //gets ACF value assoc with 'date'
    												'compare' => 'BETWEEN',
    												'value' => $year // gets value from $arr, which gets value from dropdown
    												),
    											array(
    												'key' => 'subject',
    												'value' => $subj,
    												'compare' => '='
    											)
    								)
    				); 
    		break; 
    
    		case (isset($arr["year"]) && isset($arr["author"])):
    		$year=intval($arr["year"]);
    		$author=$arr["author"];
    
    					$args = array(
    			'numberposts' => -1,
    			'post_type' => 'wcmc',
    			'meta_query' => array(
    									'relation' => 'AND',
    											array(
    												'key' => 'date', //gets ACF value assoc with 'date'
    												'compare' => '=',
    												'value' => $year //val of dropdown
    											),
    											array(
    												'key' => 'author_name1', //gets ACF value assoc with 'author_name1'
    												'value' => $author, // gets value from $arr, which gets value from dropdown
    												'compare' => '='
    											)
    								)
    				); 
    		//print_r("year and author");
    		break;
    
    		case (isset($arr["keyword"])):
    		$kywd=strtolower($arr["keyword"]);
    		$getTerms = get_terms('wcmc_keywords','hide_empty=0');
    		//$name_getTerms = $getTerms->name;
    		//print_r("all years and keyword");
    						/*$args = array(
    					'numberposts' => -1,
    					'post_type' => 'wcmc',
    					'meta_key' => 'kewywords',
    					'meta_value' => $kywd
    				); */
    		
    				$args=array(
    					'post_type'=>'wcmc',
    					'tax_query'=>array(
    						array('taxonomy' => 'wcmc_keywords',
    						'field' => 'slug',
    						'terms' => $kywd)
    						)
    					);
    		break;
    
    		default:
    		print_r("there has been an error");
    		break;
    
    	}
    ?>
    
    <div class="mobileScroll">
    <a href="#" class="mobileNavTriggerLarge" style="display: none;"></a>
    
    	<div id="main">
    
    		<div id="primary">
    			<div id="content" role="main">
    
    				<?php
    				 
    				// get results
    				$the_query = new WP_Query( $args );
    				 
    				// The Loop
    				?>
    				<?php //logit( $the_query->request, '$the_query->request:' ); ?>
    				<?php if( $the_query->have_posts() ): ?>
    					<ul>
    					<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    						<li>
    							<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    						</li>
    					<?php endwhile; else: ?>
    					sorry no results are available.
    
    					</ul>
    				<?php endif; ?>
    				 
    				<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    
    			</div><!-- #content -->
    		<?php get_sidebar(); ?>
    			<div class="clear"></div>
    
    		</div><!-- #primary -->
    
    	</div>
    
    	<?php

    As you can see above, the solution I use is very similar to the tutorial here ( http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/ ), except that I am using a switch structure to return a different $args based upon which form they use (for instance, ‘year and subject’ vs ‘year and author’).

Viewing 1 post (of 1 total)

The topic ‘How to filter query posts by date, when user input is not in yymmdd format?’ is closed to new replies.