Date_query problems

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
/select

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

?php
    $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.

Topic date-query Wordpress search

Category Web


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">
...
</select>

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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.