在for循环中循环时不能放入wp_query

I have a widget that displays a calendar, and in one of the functions I have a method called getWeekDays, which displays all the days of the week.

Inside that method, I run a sql query to display a list of posts from the current month selected in the calendar, then store all the post dates in an array.

function getWeekDays() {
    global $wpdb;
    $postDates = [];

    $this->searchDate = preg_replace('/\W\w+\s*(\W*)$/', '$1', "{$this->currentMonthStart}");
    $posts = $wpdb->get_results('SELECT `post_date` FROM `wp_posts` WHERE `post_date` LIKE "%' . $this->searchDate . '%"');

    foreach ($posts as $post) {
        $postDates[] = $post->post_date;
    }

    $args = array(
        'post_type' => 'post', // the post type
        'category_name' => 'events', // name of category
    );

    $the_query  = new WP_Query( $args );

    $weekLength = $this->getWeekLengthByMonth ();
    $firstDayOfTheWeek = date ( 'N', strtotime ( $this->currentMonthStart ) );
    $weekDays = "";
    for($i = 0; $i < $weekLength; $i ++) {
        for($j = 1; $j <= 7; $j ++) {
            $cellIndex = $i * 7 + $j;
            $cellValue = null;
            if ($cellIndex == $firstDayOfTheWeek) {
                $this->currentDay = 1;
            }
            if (! empty ( $this->currentDay ) && $this->currentDay <= $this->currentMonthDaysLength) {
                $cellValue = $this->currentDay;
                $this->currentDay ++;
            }
            $weekDays .= '<li>' . $cellValue . '</li>'; // here I print all the week days
        }
    }
    return $weekDays;
}

Now, I want the following for example: If there is a post on the 3rd, the number 3 must have an href and I should be able to access all the post details for that day.

I tried adding the wp_query inside the for loop that is used to display the days but it gives an error.

How can I achieve this?

EDIT:

The following is what I tried:

        $weekLength = $this->getWeekLengthByMonth ();
        $firstDayOfTheWeek = date ( 'N', strtotime ( $this->currentMonthStart ) );
        $weekDays = "";
        for($i = 0; $i < $weekLength; $i ++) {
            for($j = 1; $j <= 7; $j ++) {
                $cellIndex = $i * 7 + $j;
                $cellValue = null;
                if ($cellIndex == $firstDayOfTheWeek) {
                    $this->currentDay = 1;
                }
                if (! empty ( $this->currentDay ) && $this->currentDay <= $this->currentMonthDaysLength) {
                    $cellValue = $this->currentDay;
                    $this->currentDay ++;
                }
                while($query->have_posts()) : $query->the_post();
                    $weekDays .= '<li>' . $cellValue . '</li>';
                endwhile; wp_reset_query();
            }
        }
        return $weekDays;

When I view the page I see the following error:

: Uncaught Error: Call to a member function have_posts()