I’ve built a custom Ajax “Load More Blog Posts” button.
To stop the button showing when there are no more blog posts to load I have a data-attribute on the button element containing the value returned by “$wp_query->max_num_pages”.
I also have a data-attribute on the same button element with the value returned by “get_query_var(‘paged’)”.
For some reason however, when I am on the search results archive page the value returned by “$wp_query->max_num_pages” is incorrect.
For example, a search query for “test” returns 4 pages of actual results but the value in my data attribute returned by “$wp_query->max_num_pages” is 7.
This means the button is still there when there are no more posts to load, when the button is clicked at this point a “no more posts” error message appears and the button remains until it is clicked enough times that the data-attributes match.
I can’t seem to find an answer to this anywhere else and I’m particularly confused by how this only seems to be an issue on my search results archive page. Any advice or solutions would be much appreciated.