Note that the constructor of WP_Query
is:
public function __construct( $query = '' ) {
if ( ! empty( $query ) ) {
$this->query( $query );
}
}
so you need a non-empty query input in your current code snippet.
Here’s a simple test example:
$query = new WP_Query( ['post_type' => 'post' ] );
wp_send_json_success( $query->posts );
but you can check out e.g. wp_ajax_query_attachments()
to see how WordPress uses WP_Query
in an ajax call.
Also check out the built-in Rest API, it might already have the query you’re looking for?
Related Posts:
- ajax category filter
- next_posts_link not working for loop called with ajax
- Ajax and WP_Query/tax_query parameter
- get data from wp-query, outside the loop & without url change
- ‘posts_where’ filter not applying ‘WP_Query’ in `wp_ajax`
- Ajax Infinite Scroll In Custom WP_Query Loop Not Working
- WP_Query class not found
- Search – Ajax – Alter Query Parameters with Pagination
- How to wp_query posts with ajax
- WP_Query orderby breaks when using AJAX?
- Custom Loop in Page Admin Causing Other Fields to Fail
- Update Loop with Form
- WP_Query & AJAX
- Sticky Post default functionality with WP_Query is ignored when using AJAX
- Refine/Filter WP_Query Posts with jQuery or Ajax
- Show selected images on top in Media Manager
- Create unique page without header.php for json feed
- WP_Query ajax loader detect end of posts
- wp_query not searching with apostrophe
- WP Pagination on Posts Search Results Page resulting from AJAX WP Query
- Trouble with wp_reset_postdata() in Admin Panel
- Using Ajax to load more posts | Help me change the wording on my button to notify the user
- Include custom field in Ajax search WordPress
- Ajax Pagination on Ajax filter
- Wp_query returning only one post while query through ajax
- Add quicklink to in the Admin posts page where I can query by a meta_key
- Ajax load more posts – WP_Query parameters not working
- Load More Posts Button – AJAX
- how to handle multiple ajax wordpress queries?
- Ajax WP_Query reutrns no results on author.php
- Current post in AJAX call is always zero
- Filter wordpress post categories with jquery/ajax and pagination?
- Ajax future single post query doesn’t work when NOT logged in
- how update data through ajax and jquery
- Error with function in functions.php?
- Use Ajax To filter posts?
- I need help using pre_get_comments to limit comments in the comments admin screen
- Change AJAX filter from POST to GET for URL Parameters
- Loading two different AJAX requests on two different pages
- AJAX Breaking Offset Argument In WP Query
- Getting the post terms ‘wp_get_post_terms’ per post when within the functions.php file
- Apply filters to main query instead of creating new one?
- How to pass query string vars between admin pages?
- WP_Query offset is returning post from prevois loop
- Does WP_Query ‘responds’ badly to empty arguments?
- Retrieve data from wordpress db via input and put those result in dropdown
- WordPress dynamic AJAX query
- ajax wp_query conditional tags not working
- I need to run a AJAX Fuction from within a WP_Query but only works on first item
- Change post query onclick
- Add AJAX “Load more” on custom query block
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- How to filter a static post page with ajax and $wp_query
- wpquery via ajax
- Ajax (jquery) wp_query pagination returns -1
- Admin: Custom Query Returning Permissions Error
- How to pass the current content of $wp_query to a new page?
- JOIN filter doesn’t work in WP_Query in wp-ajax calls
- WordPress AJAX Request returns 400
- WordPress Ajax form filter | Search by title with ajax form, breaks the other filters
- Why when I using infinity scroll the one post loads over and over?
- Using Ajax and WP_Query to load more posts on category page fails
- WordPress live search and filter custom fields
- WordPress live search and filter
- Filter admin ajax data by url query
- WooCommerce | AJAX | Product Pagination | Help me implement Ajax Pagination
- Move some posts to end of sort order, even if there is a sort in the wp_Query already
- Finding post ID dynamically on click
- Ajax buttons not working properly in WooCommerce when using wc_get_template_part
- Load WP Query with Ajax
- remove_action – pre_get_posts – does not restore original query
- Is the ‘parse_query’ filter still valid to change filtered content on admin list view?
- How to set selected attribute on option after filter query?
- Order & Orderby clause not working with custom query
- WordPress conflict with multiple load more posts functions on click
- Issue generating custom filter for cpt admin columns
- WordPress custom query by archive title
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- modifying the loop multiple times with arguments passed through ajax to wp_query
- Modify query to exclude certain ids and certain parents and its corresponding children
- Fetching $_POST from Page Template into functions.php
- WP_Query for liked posts is showing all posts if none are liked
- Declare inline background image in functions.php
- How to get query results for the next page
- How to solve this script problem in theme directory?
- I wan to process the following js to process the AJAX Request on my function to calculate author Total Post views
- WordPress WP_Query offset parameter not working with search parameter
- Why is $wp_query fetching nothing?
- Inline Ajax call after term is appended with a @ or # etc. displayed in div
- Fire query on ajax post url page
- Sorting Posts Based On Meta Value Using AJAX
- How return normal search result using wp_ajax action for wordpress
- Duplicate posts showing up in loop using infinite scroll
- Adding arguments to WooCommerce Product Loop using AJAX
- Refresh wp_query with no changes
- WordPress WP_User_Query ignores the `meta_query` when called from an Ajax function
- AJAX search function resets wp_query vars
- WordPress Ajax search filter on dropdown select
- AJAX WP_Query’s order and orderby parameters not working
- AJAX multiple search boxes not merging with array merge