Media attachments in index not updated when associated post is changed
When attachments are included in an index and the associated post for that attachment (post_parent in the db) is changed, the index does not update to reflect that change.
I'm able to temporarily work around this by calling wp_update_post() on the attachment using the "wp_media_attach_action" action but it seems like something the plugin should do automatically.
Hi,
Trying to picture an example state of what you're working with here.
Are you saying you have media items, the attachment post type getting indexed as well, I assume intentionally, and the post they're attached to are getting updated somehow, but the attachment object is not?
Or are you say editing the featured image only, and clicking save, and that's not updating the index to reflect the newly chosen image?
Or something else? An outline of steps and things being edited would help me mentally map.
You can see all the post based action hooks that we "listen" to over at https://github.com/WebDevStudios/wp-search-with-algolia/blob/2.8.2/includes/watchers/class-algolia-post-changes-watcher.php#L108-L131
Sure thing! So we have a searchable media library that's displaying attachments that have been flagged as downloadable. The issue we noticed with the parent post association is that the permalink for that attachment changes when the parent has changed but the indexer does not detect this update.
For example, if I upload a media file to the media library without attaching it to a post the permalink would be something like this:
https://www.mysite.com/mynewfile/
If I then attach that media file to a post via the Media Libarary list page, the permalink changes to include that post as the base:
https://www.mysite.com/path/to/post/attachment/mynewfile/
Because this is done outside of the normal update post process none of those actions in the watcher you linked fire, so Algolia doesn't pick up on the change and continues to show the original permalink.
Is the process you outlined above the typical process you take?
I'm curious if an action hook in that class file I linked to earlier, that passes the index property, could help for some of these edge cases that is a bit out of normal hook processes.
If there was a simple way to trigger a reindex on a specific post that would solve the issue as well.
I know that I have an example of updating a post upon comment saving over at https://github.com/WebDevStudios/algolia-snippet-library/blob/main/indexing/reindex-on-comment-save.md
In that example there's a fair amount of scaffolding going on, so if we could provide a way for others to receive an object variable holding the index being pushed to, so that they just had to do $index->sync( $id ) in their own code, that'd provide some ease.
The question would be how...
That said I could see our sync on comment code maybe being a good starting spot for you right now.