简体   繁体   中英

WordPress query posts by date input type

I am trying to display certain posts of a certain date that users can pick via a html date input type.

This is my date input in my html:

<form name="form" action="" method="get">
    <input type="date" name="date" id="date" />
</form>

And I am trying to get the value like this:

$date = $_GET['date'];
echo $date;

But I get this error:

Undefined index: date

Anyone has any idea how I can get the selected date? I am guessing the form still needs to be submitted, is there any way I can auto submit after selecting a date? Preferably without using JS. Many thanks in advance!

How you will get date parameter by submit button or using jquery

i mean to say how the form is submitted without submit button

<form name="form" action="" method="get">
    <input type="date" name="date" id="date" />
</form>

You must check if the form's submited or not, example:

if (isset($_GET['date'])) {
   $date = $_GET['date'];
} else {
   $date = date('d/m/Y');
}

You are probably trying to get the value of the date field before you have submitted the form.

Try the following:

if (isset($_GET['date'])) {
    $date = $_GET['date'];
    echo $date;
}

You can't make the form submit automatically without using some form of Javascript.

For example:

<form name="form" action="" method="get">
    <input type="date" name="date" id="date" onchange="this.form.submit();" />
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM