Got it working! It’s not pretty but it’s functioning.
$Y = get_the_time( 'Y' );
$M = get_the_time( 'm' );
$earlier_post = get_posts( [
'date_query' => [
'before' => [
'year' => $Y,
'month' => $M,
'day' => 1,
],
],
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 1,
'ignore_sticky_posts' => 1
] );
$prev_label = get_the_time( 'F', $earlier_post[0] ) . ' ' . get_the_time( 'Y', $earlier_post[0] );
if ( ! empty( $earlier_post ) ) {
$previous_month_link = home_url( get_the_time( 'Y', $earlier_post[0] ) . "https://wordpress.stackexchange.com/" . zeroise( get_the_time( 'm', $earlier_post[0] ), 2 ) );
echo '<div class="nav-prev"><a href="' . $previous_month_link . '"><span class="nav-title">' . $prev_label . '</span></a></div>';
}
$next_post = get_posts( [
'date_query' => [
'after' => [
'year' => $Y,
'month' => $M,
'day' => date( 't', mktime( 23, 59, 59, $M, 1, $Y ) ),
],
],
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'ignore_sticky_posts' => 1
] );
$next_label = get_the_time( 'F', $next_post[0] ) . ' ' . get_the_time( 'Y', $next_post[0] );
if ( ! empty( $next_post ) ) {
$next_month_link = home_url( get_the_time( 'Y', $next_post[0] ) . "https://wordpress.stackexchange.com/" . zeroise( get_the_time( 'm', $next_post[0] ), 2 ) );
echo '<div class="nav-next"><a href="' . $next_month_link . '"><span class="nav-title">' . $next_label . '</span></a></div>';
}