1

I just have a general question about the post_class(); function.

As it gets category and tag classes relating to a post, is it database/server intensive to call this on a category page, or even a post page?

I feel as though the amount of information it gets about each post, it would have to make a number of queries to the database - just trying to ascertain if it is worth it or not.

Would it affect the page load in any way?

Thanks, bhtech

  • There's no such thing as a free function call, even echo ''; will affect page load times, albeit in an insignificant way – Tom J Nowell Jun 24 '15 at 1:47
2

For each extra functionality, you are going to pay for How much would depend on the function or action. It is like @TomJNowell said, even a simple echo statement costs time and memory although it is really minute and insignificant.

The real issue here is db calls as they can get very expensive depending on the size and nature of the query and this drastically increase page load times, so you would want to reduce unnecessary db calls or make use of transients and caches to store information from db calls in order to increase performance

Wordpress has good cache system in place when it comes to querying the db in order to make a query more effecient. Dig around in the WP_Query class for starters and see what data is cached on a query. I'm not going to go into that now, but there are are caches that stores post meta data and taxonomy term data to help with effeciency when quering this extra post info inside a post. Just for some insight, see my answer to this question: Custom post meta field effect on the performance on the post. You should be able to run the same exact tests on this issue, and that is what I did.

When looking at the post_class() source code, you would expect it to slow the page down due to extra db calls, and that is where you are wrong. As I said, post results from a query gets cached. And that is where you answer lay. As I said, I did run a quick test or two on a set of posts (10 to be exact), and here are my results

  • With post_class() functionality, I got 18 db calls in around an average time of 0.38 - 0.40 seconds

  • Without post_class() functionality, I got 18 db calls in around an average of 0.38 - 0.40 seconds

So the results are exactly the same. I have explained in the linked answer how you can also quickly set up a testing enviroment to test stuff like this, so be sure to try this out yourself

I would also encourage you to download and install a plugin called Query Monitor (to which I don't have any affiliation to) on your local test intallation. This will print out a list of queries that is executed on every page load with the exact time it takes to execute each query. It also shows page load times (which I did not have a look at during my tests). This will also give you a perfect insight on how effecient your queries and page really is. I must stress this however, do not use this plugin on a live install as it prints everything directly to screen. This will look seriously wierd and scary to someone visiting your site with the plugin installed :-)

To answer your question about whether post_class() is worth it or not, my answer would be YES. It gives you so much more control and freedom styling certain elements differently from one post to the other. You can also target specific pages or posts, terms, custom post types etc styles with css rather than using php. I personally think that omitting post_class() is like going fishing with a rod without hook, line and sinker :-)

EDIT

Thanks to the info from @birgire in comments, the plugin Query Monitor is only loaded for admins in single installs and super admins in multisite installs. You can check out their FAQ section here

  • Awesome - thanks for all that info. I did run similar tests - with and without post_class(); - and got the same results you did above. But I had trouble believing it to be true - which led me to asking the question. But obviously Wordpress is more efficient then I give it credit for. Thanks, Taylor – user71633 Jun 25 '15 at 2:51
  • My pleasure. Enjoy :-) – Pieter Goosen Jun 25 '15 at 3:47
  • I agree post_class() is a must ;-) I think the output from Query Monitor is restricted to site admins. Update: I found a note about it here in the plugins' FAQ. – birgire Jun 25 '15 at 9:25
  • Thanks @birgire for that info. Have never looked at the faq section, neiter have I taken note of this while testing. ;-) – Pieter Goosen Jun 25 '15 at 9:40
  • 1
    @birgire agreed. My opinion is, debugging, development, performance testing of code and page load times should be done on a local test install. If you then have problems online, it can only be server related. ;-) – Pieter Goosen Jun 25 '15 at 10:08

Your Answer

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