Snippet for checking if any published posts exist
-
I had an issue where I wanted to display “archives” and “3 latest stories” in the sidebar accross all pages, unless there were no posts at all. I found that on a failed search results (where there were no results), my sidebar elements disappeared. This is because there was a bit of a clash when using “have_posts()”.
I couldn’t find any solutions online that would check if any published posts existed without conflicting with the search results checks. So I wrote this very simple function:
// Check if there are any published pages function publishedPages() { return (wp_count_posts()->publish > 0) ? true : false; } //Usage: if (publishedPages()) { echo "There is at least one published post on this site"; }I’m sure there are various other solutions. But I hope that one helps someone in the future.
Mark
The topic ‘Snippet for checking if any published posts exist’ is closed to new replies.