Is it possible to run pre_get_posts inside the ajax callback? I want to change the query after the ajax run
here my code
function repeater_dynamic_query( $query ) {
global $post;
if ( $query->query['post_type'][0] == 'post' ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'london',
'include_children' => false
)
) );
$query->set( 'orderby', 'rand' );
$query->set( 'post__not_in', array($post->ID) );
$query->set( 'no_found_rows', true );
}
}
Here my ajax callback
add_action( 'wp_ajax_my_ajax_action', 'my_ajax_action_callback' );
function my_ajax_action_callback(){
add_action( 'pre_get_posts', 'repeater_dynamic_query' ); //run pre_get_posts
wp_die(); // required. to end AJAX request.
}