get_adjacent_post() failing on bulk-created posts

While working with a custom post type and a custom taxonomy, I was having trouble with get_adjacent_post(). No matter what I tried, if I limited the function to the same term, it returned empty.

At first I thought it was because I hadn’t specified the taxonomy, but when I did that and it still didn’t work, I decided to dig into the SQL queries to see what was happening under the hood.

This caught my eye: WHERE p.post_date > '2015-05-19 11:42:53'

A little poking around, and sure enough, I found the problem (and I figured out why Google had been no help—I was looking for issues related to custom post types and it turns out, that had nothing to do with it).

The actual problem: I created the posts with BulkPress, and because I was creating a relatively small number, they were all created within a single second (if there had been a ton and it had taken longer than a second, the function would still have acted funny, but then I would have been searching for things like “get_adjacent_post skipping posts custom post type” 😉 ).

Essentially, there were no other posts in the same term + taxonomy that were created either before or after the post in question (despite the fact that there were 12 posts)—because they were all created at the exact same time as far as the database was concerned (since it stops at whole seconds).

Sarah Lewis

Sarah builds websites and systems with equal ardor, and she's at her happiest when waffling something unexpected. She's anxiously awaiting solar freakin' roadways and also transporters, and doesn't much care for writing about herself in the third person.

Leave a Reply

Your email address will not be published. Required fields are marked *