0

I am trying to use a drop down for users to select and show posts made latest, last 7 days and monthly.

Currently I was able to find code to just sort by date in ASC and DESC order.

<div id="sortby"> SORT BY: &nbsp;
    <select class="dropdown-class" name="sort-posts" id="sortbox" onchange="document.location.search=this.options[this.selectedIndex].value;">
    <option disabled>Sort by</option>
    <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == 'date' && isset($_GET["order"]) && trim($_GET["order"]) == 'DESC' ){ echo 'selected'; } ?> value="?orderby=date&order=DESC">Newest</option>
    <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == 'date' && isset($_GET["order"]) && trim($_GET["order"]) == 'ASC' ){ echo 'selected'; } ?>  value="?orderby=date&order=ASC">Oldest</option>
    </select>
    </div>

What I would like to do is add to that and show posts from the last 7 days and posts within the month. Any ideas how I would get this to work?

1 Answer 1

0

The request to the server is in this phrase: value="?orderby=date&order=DESC">. That is a WP_query string. You would use a date_query with the after argument to construct a query that will return posts which were published after a certain date. Note that the string will be different every time, since it depends on the current date and time.

Once you have the query, you can pass it through add_query_arg to retrieve a string which you can include in your dropdown option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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