pantheon-systems / solr-power

A WordPress plugin to connect to Pantheon's Apache Solr search infrastructure, or your own!
https://wordpress.org/plugins/solr-power/
GNU General Public License v2.0
126 stars 60 forks source link

Adapt posts_pre_query() return values based on 'fields' argument #522

Closed danielbachhuber closed 2 years ago

danielbachhuber commented 2 years ago

From #521

bamadesigner commented 2 years ago

Hey @danielbachhuber! I saw the update! Thank you!

But it still doesn't work for me.

So the page post type is hierarchical and when the page posts are queried for the "Edit pages" admin page (/wp-admin/edit.php?post_type=page), core sets the fields query parameter to id=>parent.

As seen here: https://github.com/WordPress/WordPress/blob/9131d74641c4c857a1bc0a7de8a7b3abfdc561f3/wp-admin/includes/post.php#L1232

So our issue comes when searching pages in the admin.

In this plugin's posts_pre_query filter (that you edited), if id=>parent is set, the filter maps the posts array of WP_Post objects to an ID (key) > parent ID (value) array and returns that array to define $this->posts in the core query.

As seen here: https://github.com/pantheon-systems/solr-power/blob/f70e2b96889db8933d126d4156589410be8aa845/includes/class-solrpower-wp-query.php#L478-L480

So in the core query, when id=>parent is set, it loops through the posts array and needs access to each post's ID and post_parent property. Which is not available because this plugin's posts_pre_query removed them in its mapping.

Core code is here: https://github.com/WordPress/WordPress/blob/9131d74641c4c857a1bc0a7de8a7b3abfdc561f3/wp-includes/class-wp-query.php#L3057-L3075

So this breaks our page search query and I'm left wondering if there is something in our environment causing this issue, but in examining the code in relation to this plugin, along with a bit of localized debugging, it seems fairly obvious to me that the plugin is the issue.

I would very much welcome and appreciate your insight/thoughts.

danielbachhuber commented 2 years ago

So in the core query, when id=>parent is set, it loops through the posts array and needs access to each post's ID and post_parent property. Which is not available because this plugin's posts_pre_query removed them in its mapping.

Dang. That does seem incorrect. I was able to reproduce the failure with ef9a574ab9e20aeb98de6a23efd3be0d5836d124

I've added a fix with 9c9f113a7523c58f141434905af8cca212891e95 and tagged v2.3.2.

Can you see if this fixes your issue? If it doesn't, can you share more detail about the buggy behavior you're experiencing, and the debugging you've done?

bamadesigner commented 2 years ago

Your fix works! Thank you!

danielbachhuber commented 2 years ago

😅 Awesome!

Thanks for your help tracking it down 😊