When it comes to retrieving content outside of The Loop or retrieving content based on certain criteria, it’s usually a matter of setting up a custom query using WP_Query.
But sometimes, setting up a full custom query, executing the loop, then resetting the post data isn’t required.
Case in point: I was recently working on a project in which the client wanted a modal dialog to appear with the title and the text from a specific page. Easy enough: use get_page_by_title
.
But there’s one minor gotcha that I’ve seen more than a few times when using this method. Here’s how to get post content by title and avoid the pitfalls of doing so.