I need to modify search query in my theme to add date_query with 'after' param based on option value from form select.

Here's what I got so far:

Inside form my select looks like:

<select class="select-date" id="select-date">                           
    <option value="5">For the whole time</option>
    <option value="1">For one day</option>
    <option value="2">For 3 days</option>
    <option value="3">For week</option>
    <option value="4">For month</option>

Values must filter content based on date posts were posted. For example selected value of 3 gives us posts published in the last week.

Than I get some code to get value from selected and conditionals to change 'after' param in query. Here's how it looks like

    $dato_query = array();
    $date_z = $_GET['select-date'];
    if ($date_z == '1'){
        $dato_query['after'] = '1 day ago';
    elseif ($date_z == '2'){
        $dato_query['after'] = '3 days ago';
    elseif($date_z == '3') {
        $dato_query['after'] = '1 week ago';
    elseif ($date_z == '4'){
        $dato_query['after'] = '1 month ago';
    else {
        $dato_query['after'] = '5 years ago';

    $dato_query =  apply_filters( 'jm_job_search_data_query', $dato_query, $REQUEST );

    if( !empty( $dato_query ) ) {
        $dato_query['relation'] = 'AND';
        if( is_object( $query ) && get_class( $query ) == 'WP_Query' ) {
            $query->date_query->queries = $dato_query;
            $query->query_vars['date_query'][] = $query->date_query->queries;
        } elseif( is_array( $query ) ) {
            $query['date_query'] = $dato_query;

And after that I get only else state result: - 5 years ago.

So date_query is added to my search query, but it don't relays on select choices.

I hope someone can help me. Sorry for bad English.


Since your code returns the posts belonging to 5 years ago, then I assume your problem is in the conditionals, since none of them are run.

The form elements must have a name if you want to get their values in the back-end. Your select lacks a name, and you are trying to get its value by using its ID. You should use it this way instead:

<select class="select-date" name="select-date" id="select-date">

Now you can have access to $_GET['select-date'] on the server.

| improve this answer | |
  • oh, wow. Really forgot about that) Ill try to add name and will write about result im getting. Thank You Good Friend) – Andrey Vasilkovsky Aug 7 '17 at 9:18
  • Pazhalsta! It's these small things that sometimes annoy us for days :) – Jack Johansson Aug 7 '17 at 9:19
  • 1
    And all okay) Really appreciate this help) Have a nice day) With love from Ukraine, Andrey) – Andrey Vasilkovsky Aug 7 '17 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.