I was recently having a conversation with a friend and fellow WordPress developer about the best ways to go about checking if a post exists given nothing but an ID.
For those who are experienced with the WordPress API, there are a number of ways to go about doing this. Functions and classes that exists for things like this are:
- Running a direct query with
- …and so on
Each of these has its advantages and disadvantages, but of everything listed above, there are always two things I try to make sure that I do when working with simple functions (like checking on the existence of a post).
I try to make sure the function:
- Is as lightweight as possible
- Is as easy as possible to read within the context of the existing code
After all, months from now, you never know if you’ll recall why you wrote code a certain way nor will your team or your peers necessarily know why the code you selected is set in a certain way.