First things first. Do not you query_posts()
. Use WP_Query
instead, to prevent messing with the main query.
Now, as for your question. WP_Query
allows you to enter data parameters too. It even has a date query which you can check it out in the link I’ve provided.
Instead of using Admin-AJAX, you can write a simple REST-API endpoint for you that is faster, simpler, and does the same (even more!). But since the content of your template is unknown to me, I’m gonna skip this.
So, let’s turn your code into this:
add_action('wp_ajax_nopriv_japi_load_more','japi_load_more');
add_action('wp_ajax_japi_load_more','japi_load_more');
function japi_load_more(){
// Get the parameters
$year = htmlspecialchars(trim($_POST['digwp_y']));
$month = htmlspecialchars(trim($_POST['digwp_m']));
$day = htmlspecialchars(trim($_POST['digwp_d']));
// Set the date in the Query
$args = array(
'posts_per_page' => -1
'year' => $year,
'monthnum' => $month, // Number of the month, not name
'day' => $day,
);
$query = new WP_Query($args);
//Run the loop
if( $query->have_posts() ) {
while( $query->have_posts() ){
$query->the_post();
get_template_part('content',get_post_format());
}
} else {
echo "<p style="text-align: center; font-size: 15px; padding: 5px;">".__('Nothing found.','text-domain')."</p>";
}
}