Title: Search by Post Date
Last modified: August 31, 2016

---

# Search by Post Date

 *  [sd1](https://wordpress.org/support/users/sd1/)
 * (@sd1)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/)
 * I have a ULWPQSF search form which is working very successfully on my website
   but one of the filters needs to be search by post date. Is there a way that this
   can be achieved?
 * [https://wordpress.org/plugins/ultimate-wp-query-search-filter/](https://wordpress.org/plugins/ultimate-wp-query-search-filter/)

Viewing 12 replies - 1 through 12 (of 12 total)

 *  Plugin Author [TC.K](https://wordpress.org/support/users/wp_dummy/)
 * (@wp_dummy)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074540)
 * Yes, it is doable but require customization. How do you want the search date 
   to works ? Eg search by exact date, between two dates, by months, by weeks etc?
 *  Thread Starter [sd1](https://wordpress.org/support/users/sd1/)
 * (@sd1)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074569)
 * Just search by year. Sorry I should have mentioned that!
 *  Plugin Author [TC.K](https://wordpress.org/support/users/wp_dummy/)
 * (@wp_dummy)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074624)
 * Ok, so basically you need to add an input filed for the years in the search form,
   then you can add that to the query.
 * Eg:
    To add the input field to the search form, you can use `uwpqsf_form_top`(
   top of the form), `uwpqsf_form_mid` (in the middel of the form, between taxonomy
   filter and meta field filter) or `uwpqsf_form_bottom` action hook.
 *     ```
       add_action('uwpqsf_form_bottom', 'add_years_field');
       function add_years_field($atts){
          echo '<label>Years</label>';
          echo '<select name="byyear" >';//the name attribute is important, it will be used later.
          echo '<option value="2012">2012</option>';
          echo '<option value="2013">2013</option>';
          echo '<option value="2014">2014</option>';
          ....//add as many options as you want.
          echo '</select>';
       }
       ```
   
 * Then for the query part, you can use either `uwpqsf_deftemp_query` (for default
   search template), or `uwpqsf_query_args` (for ajax result template).
 *     ```
       add_filter('uwpqsf_query_args', 'add_years_query','',3);
       function add_years_query($args, $id,$getdata){
          if(isset($getdata['byyear']) && !empty($getadata['byyear']) ) {//only add the query if user enter the byyear input
             $args['date_query'] = array(
       		array(
       			'year' => $getdata['byyear'],
       		),
       	);
          }
         return $args;
       }
       ```
   
 * You can refer to the [documentation](https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters)
   to play around with the date_query arguments.
 *  Thread Starter [sd1](https://wordpress.org/support/users/sd1/)
 * (@sd1)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074677)
 * Thank you for your response and assistance.
 * I have put the code in the functions file but there are two issues:
 * – I have multiple search forms on the website and I only need to search by published
   date on one of the forms but the input field is showing up on all the forms. 
   Is there a way to identify the specific form in the code so that the input field
   only shows up on that particular form?
 * – The second issue is that it doesn’t seem to be searching by post-date. For 
   example, if I search ‘2012’, the results are posts from 2016 in descending order.
 *  Thread Starter [sd1](https://wordpress.org/support/users/sd1/)
 * (@sd1)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074679)
 * What I mean is the published year needs to be retrived from the database table`
   wp_posts`.`post_date`.
 *  [umari4](https://wordpress.org/support/users/umari4/)
 * (@umari4)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074683)
 * I’m sorry for interrupting. I would also like to be able to search by post date(
   year). Unfortunately, I’m not so familiar with coding and WordPress and don’t
   know where to put this codes you have mentioned above. If I put the code in the
   functions file, the site is not working properly anymore… – Therefore my question;
   where do I have to paste this codes exactly? Sorry, for this stupid question…
   Thank you very much for your help!
 *  Plugin Author [TC.K](https://wordpress.org/support/users/wp_dummy/)
 * (@wp_dummy)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074687)
 * [@sd1](https://wordpress.org/support/users/sd1/),
 * >  – I have multiple search forms on the website and I only need to search by
   > published date on one of the forms but the input field is showing up on all
   > the forms. Is there a way to identify the specific form in the code so that
   > the input field only shows up on that particular form?
 * Yes, you can. In the `add_years_field()` function, you can use `atts['id']` to
   identify the form id.
    eg:
 *     ```
       function add_years_field($atts)(){
         if(atts['id'] == '1234'){
          blah blah blah
         }
       }
       ```
   
 * > The second issue is that it doesn’t seem to be searching by post-date. For 
   > example, if I search ‘2012’, the results are posts from 2016 in descending 
   > order.
 * This maybe the date wasn’t add to the query properly. Maybe you are using wrong
   filter `uwpqsf_deftemp_query` or `uwpqsf_query_args`.
    Or, the select input name
   not correct as well. You can test it without the input value first, eg:
 *     ```
       function add_years_query($args, $id,$getdata){
   
             $args['date_query'] = array(
       		array(
       			'year' => 2014,//test this with different years
       		),
       	);
   
         return $args;
       }
       ```
   
 * If above codes works, then the problem is on the input not insert to the query.
 * [@umari4](https://wordpress.org/support/users/umari4/), the codes you have to
   insert to your theme’s functions.php file.
    You can turn your site [debug mode on](https://codex.wordpress.org/Debugging_in_WordPress)
   first, if there any error, you will know what cause it.
 *  Thread Starter [sd1](https://wordpress.org/support/users/sd1/)
 * (@sd1)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074688)
 * With some minor adjustments, I have it working, thank you so much for your assistance!
 * First in functions.php file, add the drop-down fields to the search form, including
   the form ID if applicable:
 *     ```
       add_action('uwpqsf_form_bottom', 'add_years_field');
       function add_years_field($atts){
         if($atts['id'] == '4863'){
          echo '<label>Years</label>';
          echo '<select name="byyear" >';//the name attribute is important, it will be used later.
          echo '<option value="2012">2012</option>';
          echo '<option value="2013">2013</option>';
          echo '<option value="2014">2014</option>';
   
          echo '</select>';
         }
       }
       ```
   
 * I changed:
 * `add_years_field($atts)()`
 * to:
 * `add_years_field($atts)`
 * and:
 * `if(atts['id']`
 * to:
 * `if($atts['id']`
 * Secondly, search the posts by published date:
 *     ```
       add_filter('uwpqsf_query_args', 'add_years_query','',3);
       function add_years_query($args, $id,$getdata){
          if(isset($getdata['byyear'])  ) {//only add the query if user enter the byyear input
             $args['date_query'] = array(
       		array(
       			'year' => $getdata['byyear'],
       		),
       	);
          }
         return $args;
       }
       ```
   
 * I removed this section of code:
 * `&& !empty($getadata['byyear'])`
 * I hope I didn’t do anything too drastic and thank you again for your incredible
   support.
 *  Plugin Author [TC.K](https://wordpress.org/support/users/wp_dummy/)
 * (@wp_dummy)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074703)
 * Yes, there were few typo in my codes. Glad that you can sort it out.
 *  [umari4](https://wordpress.org/support/users/umari4/)
 * (@umari4)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074755)
 * Thank you very much for the hint. I have inserted the code in my theme’s function.
   php file. But is that all I need to do?
 * I expected to have a new field for the years in the search form. But this is 
   not happening. Could you maybe give me another hint and tell me what to do exactly
   after inserting the codes in my theme’s function.php file. Sorry, I’m still learning…
 *  Thread Starter [sd1](https://wordpress.org/support/users/sd1/)
 * (@sd1)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074756)
 * If you insert the first block of code above in your theme’s function file, then
   the fields should show up on the search form on the front-end. If you used my
   edited code above, did you enter the correct form ID? Are you using a child theme
   and if so, did you remember to edit the child theme’s function file rather than
   the parent theme’s function file?
 * If you are still having trouble, post your function file code here and I will
   take a look. I’m not an expert but maybe I can help.
 *  [umari4](https://wordpress.org/support/users/umari4/)
 * (@umari4)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074779)
 * I works! 🙂 I didn’t realize that I had to change the ID!
    sd1, thank you very
   much for your help!

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Search by Post Date’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/ultimate-wp-query-search-filter_fcfcfc.
   svg)
 * [Ultimate WP Query Search Filter](https://wordpress.org/plugins/ultimate-wp-query-search-filter/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ultimate-wp-query-search-filter/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ultimate-wp-query-search-filter/)
 * [Active Topics](https://wordpress.org/support/plugin/ultimate-wp-query-search-filter/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ultimate-wp-query-search-filter/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ultimate-wp-query-search-filter/reviews/)

## Tags

 * [date](https://wordpress.org/support/topic-tag/date/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [post](https://wordpress.org/support/topic-tag/post/)

 * 12 replies
 * 3 participants
 * Last reply from: [umari4](https://wordpress.org/support/users/umari4/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/search-by-post-date/#post-7074779)
 * Status: not resolved