如何通过jQuery Datepicker搜索帖子?

时间:2018-10-08 作者:edgematrix

我正在WordPress网站上工作,我有自己的搜索筛选页面,在那里我添加了jquery日期选择器,现在我想按日期选择器搜索帖子?

Html Code

script>
$(function() {

   jQuery( "#datepicker-13" ).datepicker({
       dateFormat : "yy-mm-dd"
   });
   $("#datepicker-13").datepicker().datepicker("setDate", new Date());
});
</script>

<input type = "text" name="datefrom" class="form-control alertform" id = "datepicker-13">

Wordpress Query

$datefrom = $_POST[\'datefrom\'];

if(!empty($datefrom) && $datefrom !==\'\')
{
   $args    = array(
   \'post_type\' => \'post\',
   \'posts_per_page\' =>8,
   \'paged\' => $paged,
   \'post_date\'  => $datefrom,
   \'post_status\' => \'publish\' 
    );
}
 $my_query = new WP_Query($args);
这里是它的解决方案

$datefrom = $_POST[\'datefrom\'];
$time  = strtotime($datefrom);
$day   = date(\'d\',$time);
$month = date(\'m\',$time);
$year  = date(\'Y\',$time);

$args = array(

    \'post_type\' => \'post\',
    \'posts_per_page\' =>8,
    \'paged\' => $paged,

\'date_query\' => array(
    array(
   \'year\'  => $year,
   \'month\' => $month,
   \'day\'   => $day,
),
),
);

1 个回复
SO网友:Rachid Chihabi

我认为最好使用date\\u query作为参数,如下所示:

$query = new WP_Query( \'year=2012&monthnum=12&day=12\' );

or:

$args = array(
    \'date_query\' => array(
        array(
            \'year\'  => 2012,
            \'month\' => 12,
            \'day\'   => 12,
        ),
    ),
);
$query = new WP_Query( $args );
资料来源:https://codex.wordpress.org/Class_Reference/WP_Query

结束