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://www.ads-software.com/plugins/ultimate-wp-query-search-filter/
-
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?
Just search by year. Sorry I should have mentioned that!
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 useuwpqsf_form_top
(top of the form),uwpqsf_form_mid
(in the middel of the form, between taxonomy filter and meta field filter) oruwpqsf_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), oruwpqsf_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 to play around with the date_query arguments.
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.
What I mean is the published year needs to be retrived from the database table
wp_posts
.post_date
.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!
@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 useatts['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
oruwpqsf_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, the codes you have to insert to your theme’s functions.php file.
You can turn your site debug mode on first, if there any error, you will know what cause it.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.
Yes, there were few typo in my codes. Glad that you can sort it out.
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…
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.
I works! ?? I didn’t realize that I had to change the ID!
sd1, thank you very much for your help!
- The topic ‘Search by Post Date’ is closed to new replies.