0

I'm using a custom query to filter posts by date range, i read the documentation and i searched for many examples, but this code seems right to me and i don't know why it is not working ! please help it's making me crasy :(

function my_pre_get_posts( $query ) {

    // do not modify queries in the admin
    if( is_admin() ) {

        return $query;

    }


    // only modify queries for 'event' post type
    if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'pubmed' ) {

        if( isset($_GET['date_deb'] ) && isset($_GET['date_fin'] )  ) {

            $date_debut = strtotime("01-01-".$_GET['date_deb']);
            $date_fin = strtotime("31-12-".$_GET['date_fin']);

            $date_debut = date('Ymd',$date_debut);
            $date_fin = date('Ymd',$date_fin);

            //echo $date_debut->format("Y");
            $query->set( 'meta_query' , array( 
                                            array(

                                                'key' => 'datecreated',
                                                'value'   => array( $date_debut , $date_fin ),
                                                'compare' => 'BETWEEN',
                                                'type'    => 'NUMERIC',
                                            )
                        ));

        } 

    }

    // return
    return $query;

}

add_action('pre_get_posts', 'my_pre_get_posts');
2
  • PS: I'm filtering by "Year range" and then used a tweak to compare two dates instead (as it shows in my code). – hammed Jun 7 '17 at 12:32
  • I found out that my code is right, but it works only if i save the post manually from the backend, because i was importing posts from an xml file and saving custom fields with the ACF update_field() function, so it's not actually saving the custom field in the same way default wordpress custom fields works, so when i create a query with a custom field filter, it does not work because it's finding it empty field unless you save the post again manually. – hammed Jun 8 '17 at 11:00
1

Probably the date format should be different - like:

        $date_debut = date('Y-m-d',$date_debut);
        $date_fin = date('Y-m-d',$date_fin);

I haven't tested this but the doc says:

The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format.

1
  • thaks for your response, but I found out that my code is right, but it works only if i save the post manually from the backend, because i was importing posts from an xml file and saving custom fields with the ACF update_field() function, so it's not actually saving the custom field in the same way default wordpress custom fields works, so when i create a query with a custom field filter, it does not work because it's finding it empty field unless you save the post again manually. – hammed Jul 26 '17 at 14:29

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.