I run a website for a company with a careers section, with each vacancy posted as a custom post type. When vacancies are available, ie a post type of "vacancy" is published, we want to display a ribbon in the top corner, directing people to the careers page.

To check for published posts obviously I could get_posts for that post type, but that seems a bit inefficient as I'll be battering the database on every page load to check. Is there any more DB efficient way of doing this, kind of like a have_posts( 'post_type=vacancy' ) ?


I wouldn't worry about doing a second query (e.g. using get_posts()). If you want to check if a post type has any published posts - you're going to have to query the database. But reading a database is really quick - any noticable delay in your page load is more likely to come from extra HTTP requests (images, javascript, css etc).

In any case, if you are worried about speed, you presumably have some sort cache plug-in enabled, in which case, most of your visitors won't notice a difference. In fact, you entire page will only be generated if the cache has expired.

  • Cool, thanks for that. I hadn't noticed any particular slowdown, I'm just always keen to keep it talking to the database as little as possible. And yea we are running a caching plugin. – artparks May 9 '12 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.