What I see is that you have moved all messages to their own table: wp_fep_messages
That changes a lot. So for Gravity Forms what I need to understand is how attachments are assigned to messages now.
For OpenEMR since all messages are in their own table now I will need to completely re write the query for that table obviously. I am wondering about your use of the wp_postmeta table. In the old system it was being used to track participants, deletions of messages with pm_delete and _fep_delete_by_; then clearing of messages with pm_clear.
Since we are no longer using the post table, p.post_type = ‘fep_message’ is not necessary. That should simplify the query.
Am I on the right track?