How to hook the pre_get_posts filter via ajax call

You can apply a custom filter on your WP_Query that is on your ajax callback function like this:

$my_query = apply_filters('my_plugin_pre_get_posts', (new WP_Query($args)));

Now you can use your existing pre_get_posts function for ajax calls also,

add_action('pre_get_posts', 'myplugin_filter_posts'); // For All WP_Query instances
add_filter('my_plugin_pre_get_posts', 'myplugin_filter_posts'); // For ajax

function myplugin_filter_posts($query) {
    if (defined('DOING_AJAX') && DOING_AJAX) {
        // Your code executed only on ajax requests
    }

    // Codes here Execute everywhere

    return $query; // Return $query is required for the filter we defined
}

You can tweak it to achieve what you wish

tech