I'm still new to WordPress and PHP programming in general, so I'll appreciate feedback and comments on this code I wrote.
For a client's website, it is essential that their Single event view (single.php) has "Previous" and "Next" navigation links at the top of the page. I first simply threw in navigation codes using WordPress' previous_post_link() and next_post_link() functions. I soon learned it doesn't work well because it uses post_date chronology rather than _EventStartDate. I came up with the following solution:
I originally tried to search for the original post by using the WordPress ID key. I thought it would be the easiest and most efficient way. But it seems that the PHP array_search() function doesn't dig into an array of arrays as I thought it would, so I had to search using the entire post (stripped of its 'ancestors' key and field). The code above does work. However, I would appreciate it if anyone can give me some insight on why I couldn't simply search the $queryPosts array using the current post's ID key. Thanks in advance!
Also, as a side note, I found that I couldn't use the posts 'guid' field to get the URLs. The 'guid' is calculated at the time of the post, and when I moved the site, the URLs for older posts still points to the old location. This is why navigation links are calculated using WordPress' default