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
Related Posts:
- WordPress get_posts function, 1300 lines long
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- Use of caller_ get_ posts
- Tax_query terms ID’s using variable
- Initiating WP_Query() with no args and adding them later through the query() method
- Pagination custom query
- WP_Query and name__in
- Order posts by tags count?
- Querying Multiple Custom Taxonomy Terms
- WP Query with offset breaks wp_pagenavi or any pagination
- Show list of authors with latest post NOT older than a month
- Unable to combine “search value” and “tax_query” using WP_Query
- How to extend tag and category “Related Posts” query to custom post_type if the first 2 terms have no posts
- WP_Query or get_posts?
- WP_QUERY returns empty set when fired from a WP-CRON scheduled event
- Interupting the loop with extra divs to display data, and how to get it to work responsively
- WP_Query, tax_query and term_meta: How to?
- Custom category search box for WordPress
- How to pass many ids in post__in?
- Sub-loop / nested loops Best Practices
- How to echo data after a WP_Query
- Query all published post AND attachment with specific mime type
- WP_Query returns random number of posts
- How do I run through a WordPress loop called from a filter function?
- How do I get the title of a category in a custom loop?
- Load More Posts Button – AJAX
- Is it a good idea to improve meta query performance by adding tax query?
- WP_Query: Group events by year, sorted DESC; then by date for each year group, sorted ASC
- How to display user order by role
- How to list custom fields as headers and list all pages sharing that custom field under it?
- Best practice for query_posts in responsive design?
- hide custom post types with specific meta key ON admin backend
- Orderby listing issue
- Current post in AJAX call is always zero
- Multiple filter conditions for WP_Query
- Get all fields inlcuding “ACF” (Advanced Custom Fields) columns in wp_query
- why does not my wp_query work?
- How to access custom WordPress database tables
- WP_query : Force last posts number over Reading Settings
- Order by most used tag
- How to set an alternate posts_per_page value for default queries in different templates
- WP Query – posts only by specific user
- how can I get seperate the HTML in the_content(); output?
- Is wp_reset_postdata always necessary?
- wp_query: implicit paging? I get less posts that I should
- WP Query Meta Value – How To Identify Specific, Unique Values?
- Get user first name from custom endpoint
- Shortcode Not Working in Slider When Added To Post Title
- WP_QUERY with group by with custom fields
- Loading two different AJAX requests on two different pages
- Displaying pages on an archive page
- Multiple pagename WP Query
- How I can repeat 2 HTML templates in a WordPress Query?
- Loop categories by recent post
- How to add WordPress featured image via SQL
- Meta query with compare by more than 2 fields
- Using WHERE on a WP_Query
- Insert images into wordpress post with a query
- Offset WP_Query by negative 1
- How to apply pre_get_posts to a custom query?
- meta_query returning excluded result
- How to pass the current content of $wp_query to a new page?
- WP_Query retuning empty array in rest api
- Query post ordered by meta value that show first post that contained a tag
- WordPress Ajax form filter | Search by title with ajax form, breaks the other filters
- Passing a SQL query to the WP Query
- Why when I using infinity scroll the one post loads over and over?
- Query posts by post type, author and post meta
- Show child by slug, while knowing parent ID
- pre_user_query vs pre_get_posts
- Can I alter the main loop to ‘orderby’ a custom callback?
- How can I filter products by meta in Woocommerce REST API?
- Query for getting posts with their custom fields data in WordPress
- WP_Query not returning any posts
- WooCommerce | AJAX | Product Pagination | Help me implement Ajax Pagination
- Custom WordPress post query for displaying time-released content on website
- How to delete a complete forum topic from root folder in wordpress
- Slow Query with more 100 products
- Taxonomy archive, categorised by other taxonomy, not hiding empty taxonomies
- meta_query is not allowing to show posts when querying multiple arguments in a WP_Query
- Showing the correct number of posts_per_page when avoiding the use of post__not_in
- Migrating mysqli query to wp_query
- Second WP_Query loop shows data from main query
- My entry results are not consistently alphabetized
- WordPress Bootstrap Grid
- How to order by datediff in WP loop?
- Custom query for tag and custom tag from 2 post type
- WP_Query within save_post doesn’t include the item that triggered the action?
- Search format not matching taxonomy query
- Pagination doesn’t work for home page custom post type query
- How to display comments list by order when clicking on newest or oldest link?
- Get the number of rows from $wpdb->get_results not working
- Loop issues when creating custom query for media uploader
- Query multiple taxonomies with pagination
- Stylesheet won’t load even when linked
- How to show featured post first, then separate loop for other posts
- error with $query->have_posts()
- Multiple Orderby is not working right
- How to retrieve _embed (etc) fields in get_posts custom query?
- Using ‘meta_query’ with the ‘pre_get_posts()’ hook disables searching for post titles