There’s probably lots wrong with this - which I will come to realise on my own with some more coding knowledge. For the moment could someone PLEASE tell me why this date_query is returning values from all over the place??

I have annotated any parts I think are helpful to understand, the whole thing is for members of the admin team to grab some stats off the site in a customisable way ie last 300 subscribers or people who subscribed between two dates…

I’ve read the codex over and over and don’t see how it can be going wrong - same number / date formats….

I’ve included the workings below it - (they are obviously calculated earlier in the script.) Just making it easier for people to glance over, by seeing the query first.

    $args = array(
                  'posts_per_page' => -1,
                  'post_status' => 'publish',
                  'post_type' => 'subscription',
                  'orderby' => 'date',
                  'order' => 'DESC',
                                    'date_query' => array(
                                                          array(
                                                                'after'     => array(
                                                                                     'year'  => $BeginYear,
                                                                                     'month' => $BeginMonthNum,
                                                                                     'day'   => $BeginDayNum,
                                                                                     ),
                                                                'before'    => array(
                                                                                     'year'  => $EndYear,
                                                                                     'month' => $EndMonthNum,
                                                                                     'day'   => $EndDayNum,
                                                                                     ),
                                                                'inclusive' => true, // true or false here doesn't seem to change anything FYI?
                                                                'column' => 'post_date', // included even though default value - both these added / removed still has problem
                                                                ),
                                                          ),
                  'meta_query' =>
                  array(
                        'key' => 'state',
                        'value' => 'complete', // this is to make sure it's a confirmed & paid subscription and is important
                        ),
                  );

This is how I’ve been getting the values and logic to get to that particular query in the first place.

<?php
if (isset($_POST['update'])) {
    if ((!empty($_POST['begin'])) AND (!empty($_POST['end'])) ) { // stops WSOD for empty values passed through date boxes
$beginpost = $_POST['begin'];
$begin = trim($beginpost);
$begindate = str_replace(array('/' , ',' , '-'), '', $begin);
        if (is_numeric($begindate)) { // stops people putting Jan etc in - WSOD 
            $BeginTimestamp = DateTime::createFromFormat("Ymd", $begindate);
            $NiceBeginDate = $BeginTimestamp->format("D d M Y");
            $BeginDayNum = $BeginTimestamp->format("J");
            $BeginMonthNum = $BeginTimestamp->format("n");
            $BeginYear = $BeginTimestamp->format("Y");
        } else {
        $number = 0;
    echo '<span style="color: #ff0000;"><strong>Please provide a numeric date</strong></span>';
    }
$endpost = $_POST['end'];
$end = trim($endpost);
$enddate = str_replace(array('/' , ',' , '-'), '', $end);
if (is_numeric($enddate)) {  
    $EndTimestamp = DateTime::createFromFormat("Ymd", $enddate);
    $NiceEndDate = $EndTimestamp->format("D d M Y");
    $EndDayNum = $EndTimestamp->format("J");
    $EndMonthNum = $EndTimestamp->format("n");
    $EndYear = $EndTimestamp->format("Y");
} else {
$number = 0;
echo '<span style="color: #ff0000;"><strong>Please provide a numeric date</strong></span>';
}
echo $NiceEndDate;
$number == false;
} // end if - if both dates are full
} // end isset post 'update'
$count = 1;
if ($number == false) { // number refers to the option to do last number of subscribers rather than date range and is tallied against a different query with no date ranges in
[WORK OUT QUERY HERE]

Read more here: Whats up with this date_query?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: