The original request that loaded the page you are making the AJAX request from ended as soon as the page was sent to your browser. That connection is closed and it is gone from memory.
When you make an AJAX request, it’s a new request, so $wp_query is empty because you haven’t queried anything. You need to create a new WP_Query instance and query the database, passing whatever query variables you want to set in this query via your AJAX function.
Related Posts:
- Search – Ajax – Alter Query Parameters with Pagination
- Post_count only shows the number of results per page
- next_posts_link not working for loop called with ajax
- Pagination not working for Custom search form & custom result template
- Pagination broken when merging search results with additional WP_Query
- Pagination wont work with search results template [duplicate]
- Add custom search results to main WP search [duplicate]
- WP_Query ajax loader detect end of posts
- WP Pagination on Posts Search Results Page resulting from AJAX WP Query
- wp_query ‘s’, search filter with pagination is not working
- how to handle multiple ajax wordpress queries?
- WordPress pagination not working on template search.php
- Cutom wp_query for geolocation search, pagination not working
- How do I get my custom query to work with search results after the first page?
- Ajax (jquery) wp_query pagination returns -1
- Pagination repeating posts on search results
- Filter admin ajax data by url query
- WordPress search page pagination displaying but not functional
- Order & Orderby clause not working with custom query
- WordPress WP_Query offset parameter not working with search parameter
- Duplicate posts showing up in loop using infinite scroll
- Search results stuck on page 1
- AJAX multiple search boxes not merging with array merge
- Can I force WP_Query to return no results?
- Multiple WP_Query loops with Pagination
- paginate_links() adds empty href to first page and previous link
- Pagination custom query
- Add and in the header while looping over custom query in page template
- Pagination not working on static page
- ‘paged’ in wp_query doesn’t work
- Display different number of posts from one category on the different pages
- Pagination not working Search posts
- WP Query with offset breaks wp_pagenavi or any pagination
- WordPress pagination link always leads to home page
- WP_Query orderby breaks when using AJAX?
- Including only current user’s posts in search
- Custom category search box for WordPress
- Refine search results using WP_Query
- Search Posts with Custom Fields as query
- Pagination of a WP_Query Loop in a child-page page template
- Load More Posts Button – AJAX
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- query_vars overriding WP_Query args
- Avoid repeated post on page 2 when excluding one on page 1
- Current post in AJAX call is always zero
- Add filter post_where and passing post_type argument
- new WP_Query messes up pagination
- Pagination is not working with custom query inside a homepage template
- Display single most recent post in category on every page of category archive
- Wildcard search in WP Query
- Pagination works in custom query loop but it doesn’t work in shortcode
- Search Page Returns Nothing
- Select2 AJAX and WP Query Returns ALL and does not filter
- Redirect Search to Form When No Product Results Are Found
- Issues with search after added meta_query
- change posts_orderby of the callback wp_query?
- How to use a dropdown to filter posts by custom field
- Content not displaying on paginated page
- WP_query is not returning the expected result
- I need to run a AJAX Fuction from within a WP_Query but only works on first item
- Search.php – return number of results but cannot loop through
- Adding pagination to sub-wp_query within a singular post page
- $wp_query->found_posts not returning correct value
- How to search by slug and old slug in single API
- Reciving Error 404 blog/page/2/
- duplicated posts when using pagination
- wordpress pagination wont render on custom page template even though its right under the loop like the documentation says so
- Using Ajax and WP_Query to load more posts on category page fails
- I have problems with the search query using multiple post types
- Categories In English version showing not canonical URL, instead shows query search result
- Pagination for Category does not work
- How to debug none working search query?
- Include search tags and users in my search results system
- WordPress pagination not returning posts on second page
- Paginate nested query of child posts
- Is instantiating WP_Query not possible within an admin Ajax call?
- WordPress conflict with multiple load more posts functions on click
- Show All with Isotope
- Search Filter With Custom Taxonomy and Custom Fields : How do I handle it via plugin?
- A very strange problem with search query
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- WP_Query for liked posts is showing all posts if none are liked
- custom query – offset to pagination
- previous_posts_link not working in WordPress pagination
- Pagination not working with WP_QUERY
- Pagination in custom query in a category page
- WordPress WP_Query Array Custom search via taxonomies
- Custom post type blog pagination conflict
- wp_query pagination links producing 404
- Manipulating a query
- Can’t get pagination to work with this WP_Query loop
- Pagination in custom loop [duplicate]
- Pagination in WP query with transient API
- paginate_links() with Custom Taxonomy
- Pagination problem with multiple loops on the same page
- Pagination not working in secondary query
- WP Paginate not working properly
- Why does WP_Query not search for two ‘meta_query’ keys separated with OR?
- Custom Taxonomy in custom REST API search
- Removing filename searches when searching attachments