To Ajaxify the request you need two parts, a php function, hooked to the wp_admin_ajax ( and the wp_admin_ajax_nopriv if you want it to be available for non logged in user) and a JavaScript to request the AJAX.
PHP
add_action('wp_ajax_getpost_by_year','process_getpost_by_year');
add_action('wp_ajax_nopriv_getpost_by_year','process_getpost_by_year');
function process_getpost_by_year(){
$year = $_REQUEST['year'];
$args = array(
'post_type' => 'post',
'ignore_sticky_posts' => 1,
'year' => $year,
);
$the_query = new WP_Query( $args );
wp_send_json($the_query );
JavaScript
$.ajax({
url: 'your-wordpress-site.com/admin-ajax.php',
data: {
year: year
action: 'getpost_by_year'
},
})
.done(function(the_query) {
// do stuff with the query result here
console.log(the_query);
});
Don’t forget to update your-wordpress-site.com
to your site url, year
variable, and