0

I'm working on the WordPress website I have my own search filter page, where I added the jquery datepicker, now I want to search post by datepicker?

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);

Here The Solution Of It

$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,
),
),
);
0

I think it's better to use date_query as argument, like so :

$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 );

Source : https://codex.wordpress.org/Class_Reference/WP_Query

  • i have tried your code but not working – edgematrix Oct 8 '18 at 11:21
  • If you can't get this to work, try using a prepared query like this : global $wpdb; $result = $wpdb->get_col( $wpdb->prepare( " SELECT count(*) as number FROM {$wpdb->posts} p WHERE p.post_date = '%s' AND p.post_status = 'publish'", $your_date ) ); – Rachid Chihabi Oct 8 '18 at 11:37
  • im trying to using like this $orderdate = explode('-', $datefrom); $month = $orderdate[0]; $day = $orderdate[1]; $year = $orderdate[2]; $args = array( 'post_type' => 'post', 'posts_per_page' =>8, 'paged' => $paged, 'date_query' => array( array( 'year' => $year, 'month' => $month, 'day' => $day, ), ), ); – edgematrix Oct 8 '18 at 11:45
  • humm, i see, you have to cast your vars to be integer instead of strings, like this : $args = array( 'post_type' => 'post', 'posts_per_page' =>8, 'date_query' => array( array( 'year' => (int)$year, 'month' => (int)$month, 'day' => (int)$day ) ) ); – Rachid Chihabi Oct 8 '18 at 11:59

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.