I am at a wall here. I am updating my search result using ajax like so :
function ajax_search_enqueues() {
if ( is_search() ) {
wp_enqueue_script( 'ajax-search', get_stylesheet_directory_uri() . '/js/custom/ajax-search.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'ajax-search', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
}
add_action( 'wp_enqueue_scripts', 'ajax_search_enqueues' );
I am triggering the ajax call on an input change with this
jQuery('input[type="checkbox"]').on( 'change', function() {
var cat = jQuery(this).attr('name');
if (this.checked) {
cats.push(cat);
} else {
cats.splice(cats.indexOf(cat), 1);
}
jQuery.ajax({
type: 'GET',
url: myAjax.ajaxurl,
data: {
action: 'load_search_results',
categories: cats,
s: s
},
success: function( data ) {
jQuery( ".content").empty().append( data );
console.log(data);
}
});
} );
add_action( 'wp_ajax_load_search_results', 'load_search_results' );
add_action( ‘wp_ajax_nopriv_load_search_results’, ‘load_search_results’ );
add_action( ‘genesis_after_content’, ‘breed_posts_nav’ );
and calling the functions like so
function load_search_results() {
get_template_part( 'loop', 'grid' );
$args = array(
'post_type' => 'post',
's' => $_GET['s'],
'category__in' =>$_GET['categories'],
'posts_per_page' => 12,
);
// doing loop stuff
die();
}
I am retrieving all information properly besides pagination. The reason pagination breaks is because I am filtering by category. So on the left hand side I have category checkboxes and when a checkbox is changed it triggers the ajax request. I can get the pagination to update but when clicking on the page number the url is directed to http://dev:8888/wp-admin/admin-ajax.php?action=load_search_results/blah/blah/blah
this breaks the page and is no longer on the search.php page. Is there a way to use pagination using ajax? Any help would be appreciated.
1 comment
Comments are closed.