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 61 forks source link

Add filter to get_post result before indexing #394

Open jhned opened 5 years ago

jhned commented 5 years ago

https://github.com/pantheon-systems/solr-power/blob/e2f133c257058beb4426e819ee5ed98e17df0c54/includes/class-solrpower-batch-index.php#L228

I'm in a situation where I have many posts that have discrete, specific names. On the front-end, I filter the post title to merge the current post title with parent post titles in order to provide more context. I don't want to change the post title itself in the database, because I need to keep the data as close to the original source as possible.

For that reason, I'm looking for a way to filter the post information before indexing it, so that I can include post parent title information in what gets indexed. One way of doing this would be to filter the post object result of get_post on line 228 of /includes/class-solrpower-batch-index.php.

I'll go ahead and submit a quick pull request, but look forward to other thoughts on the matter.

pavellishin commented 5 years ago

Have you considered using the solr_build_document hook? https://github.com/pantheon-systems/solr-power/blob/master/includes/class-solrpower-sync.php#L396 <-- build_document runs the resulting solr doc and the WP post through this filter before submitting it for a solr update.

We're using it in our project to strip some content out of the body of the post, but you could use it to modify the post title as well.