$wp_query->max_num_pages returning incorrect value on blog post search archive page

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’)”.

In my JavaScript I hide the button when the value for “$wp_query->max_num_pages” matches the value for “get_query_var(‘paged’)”. This works great on the blog homepage, date archives, author archives and category archives.

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.

Read more here:: max_num_pages returning incorrect value on blog post search archive page” rel=nofollow>$wp_query->max_num_pages returning incorrect value on blog post search archive page

Leave a Reply

Your email address will not be published. Required fields are marked *