Exactly What Separates CP Themes from WP Coding?

I love ClassicPress’ reduction of database calls (six for CP vs 25 for WP.) What exactly can I do when creating a CP theme that will keep the database hits low?

3 Likes

Yes, CP should be making fewer database calls than the latest versions of WP, because we started with WP 4.9 and haven’t added new features that require extra database queries. But it’s important to be specific when measuring this:

Six database calls when doing what on CP? What version of CP?

When you get 25 database calls on WP, is it the same site, loading the same page? What version of WP?

We don’t have any specific guidelines for this. Generally keep your theme simple, and don’t make unnecessary queries, or call functions that make unnecessary queries. From what I have seen though, usually plugins are the main culprit for this kind of thing, not themes.

When we start moving features out into core plugins we should be able to reduce the number of database queries even further.

2 Likes

Core accesses the database according to the URL requested, and loads the template to handle that request. The theme calls core functions to output the data (that has already been retrieved and cached). So if the theme doesn’t do related posts or breadcrumbs or tag clouds or custom fields, etc., then there is no additional hit on the database. But the theme might want to show a menu(posts table) or author bio(user meta) or comments(comment table) or links to next and previous(posts table).

One of the things that increases queries for WP is that there are new custom post types for blocks and template parts

1 Like

I’d say (a) use CP/WP functions, where possible, instead of writing your own and (b) if you do need to write your own or do something complex, use transients so that someone using a persistent object cache on their site can usually avoid hitting the database.

I’m testing the CP 1.31 version of TwentyFifteen. The regular WP TwentyFifteen on the same CP install has the same low number of database hits. My CP site has four active plugins: ACF, Custom Post Type UI, WP SEO Structured Data Schema and wpDataTables.

I believe the 25-hit WP version was with many more plugins, so that figure may have been a bit apples-and-oranges.

On the other hand, WP 5.8.1 with TwentyNineteen and no plugins installed, needs three more database hits than CP with four plugins.

1 Like

The remark about custom post types is interesting. I make great use of CP with ACF and custom post types with no noticeable difference. A welcome surprise.

Thanks. I’m always trying to reduce the number of database calls. I’ve taken to hard-coding much of a theme, seeing little reason to hit the database just for the site name or the language.

I’ll have to brush up on transients.

yes and no…transients are stored in the database (options table, which by default is autoloaded)

Yes, I know, which is why I made the point about someone using a persistent object cache. If they are, then the database isn’t touched at all, because the object cache takes over instead. If there’s no object cache, then the transients will be loaded from the database, but faster. So there’s no loss even then.