Kill or replace the main wp query when rendering sitemaps #102

Open
mjangda opened this Issue Jan 6, 2017 · 2 comments

Projects

None yet

2 participants

@mjangda
Member
mjangda commented Jan 6, 2017

We handle querying for the sitemap post on render (i.e after the template_include hook) which means that WordPress' main query has already run, which is a waste since that query and it's results are never used.

We should probably replace the query to look up the post for single sitemaps and kill it for the root sitemap.

@mdbitz
Contributor
mdbitz commented Jan 8, 2017 edited

Hi @mjangda What are your thoughts on using the posts_pre_query filter that was added in 4.6 to "kill" the main query. We'll still have the php processing of WP_Query occur but there won't be a db hit. I've implemented the filter in my forked branch https://github.com/mdbitz/msm-sitemap/blob/fix/102/msm-sitemap.php However I'm having issues getting a proper unit test setup. going to post a message in slack to see if someone has tips.

I thought I could use go_to in the phpunit test to confirm that $wp_query was returning posts on the main page then hit sitemap.xml and confirm it returned empty but that doesn't seem to be the case.

https://github.com/mdbitz/msm-sitemap/blob/fix/102/tests/test-sitemap-core-query.php

@mdbitz
Contributor
mdbitz commented Jan 8, 2017

Mo, spoke with Dennis Snell and got some great feedback on setting up appropriate unit tests for the filter. Let me know your thoughts on if you feel posts_pre_query is the proper way to resolve the main query load and if so I'll get a pull request setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment