I’d do it like this: (see the // comment here
in the code)
// Wrap the AJAX call to `test_function` in a `function`.
function ajaxTestFunction( page_num ) {
if (jQuery('#telegram_chk').is(":checked"))
var telegram="";
else
var telegram="facebook";
if (jQuery('#twitter_chk').is(":checked"))
var twitter="";
else
var twitter="twitter";
if (jQuery('#eth_chk').is(":checked"))
var eth="";
else
var eth="ethereum";
if (jQuery('#xml_chk').is(":checked"))
var xml="";
else
var xml="stellar";
var checkboxes = [telegram, twitter, eth, xml]
jQuery.ajax({
url : rml_obj.ajax_url,
type : 'post',
data : {
action : 'test_function',
security : rml_obj.check_nonce,
test_data : checkboxes,
paged: page_num || 1
},
success : function( response ) {
jQuery('#result').html(response);
}
});
}
// In the `change` callback, we call `ajaxTestFunction()`.
jQuery( '#content' ).on( 'change', '#telegram_chk, #twitter_chk, #eth_chk, #xml_chk', function( e ) {
// Starts at page #1
ajaxTestFunction();
});
// And add a listener/callback for the pagination clicks.
jQuery( '#result' ).on( 'click', '.pagination a', function( e ){
e.preventDefault();
var paged = /[\?&]paged=(\d+)/.test( this.href ) && RegExp.$1;
ajaxTestFunction( paged );
});
And in the PHP test_function()
function, capture paged
like this:
$paged = $_POST['paged'];
..and not:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
Additional Note
Why don’t you utilize the paginate_links()
function in WordPress? =)
The code starting from this line:
$pages =$q->max_num_pages;
..up until the }
above this:
}
else {
echo "Sorry, we have not found any posts.";
}
..can be replaced with this:
$links = paginate_links([
'base' => '%_%',
'format' => '?paged=%#%',
'total' => $q->max_num_pages,
'current' => $paged,
]);
if ( $links ) {
echo '<div class="pagination">';
echo $links;
echo '</div>';
}
See https://codex.wordpress.org/Function_Reference/paginate_links and/or https://developer.wordpress.org/reference/functions/paginate_links/.