Cache the registration of a custom post type?

admin2025-06-05  1

In computation, cache is used to store the state of something that is complex to generate. We then use the cached version for future requests.

Taking a look inside the register_post_type function, I see that it does a lot of stuff:

// ...
$post_type_object = new WP_Post_Type( $post_type, $args );
$post_type_object->add_supports();
$post_type_object->add_rewrite_rules();
$post_type_object->register_meta_boxes();
$post_type_object->add_hooks();
$post_type_object->register_taxonomies();
// ...

Since register_post_type is hooked at init, it runs on every request. If the CPT uses metabox, you'd have add_meta_boxes running as well, and so on. Depending on the complexity of the CPT, this can get really messy.

I think this is a great candidate for caching. It runs on every request, and it's complex to generate.

That being said, is it possible to cache the creation of the Custom Post Type object in WordPress?

Preferably:

  • Without the use of cache plugins or third-party software.
  • Caching not only the post, but the metaboxes related to it, etc, for optimal performance at benchmarks, etc.

In computation, cache is used to store the state of something that is complex to generate. We then use the cached version for future requests.

Taking a look inside the register_post_type function, I see that it does a lot of stuff:

// ...
$post_type_object = new WP_Post_Type( $post_type, $args );
$post_type_object->add_supports();
$post_type_object->add_rewrite_rules();
$post_type_object->register_meta_boxes();
$post_type_object->add_hooks();
$post_type_object->register_taxonomies();
// ...

Since register_post_type is hooked at init, it runs on every request. If the CPT uses metabox, you'd have add_meta_boxes running as well, and so on. Depending on the complexity of the CPT, this can get really messy.

I think this is a great candidate for caching. It runs on every request, and it's complex to generate.

That being said, is it possible to cache the creation of the Custom Post Type object in WordPress?

Preferably:

  • Without the use of cache plugins or third-party software.
  • Caching not only the post, but the metaboxes related to it, etc, for optimal performance at benchmarks, etc.
Share Improve this question asked Dec 25, 2018 at 14:14 Lucas BustamanteLucas Bustamante 2,3681 gold badge28 silver badges43 bronze badges 4
  • Why do you think it’s complex to generate? – Krzysiek Dróżdż Commented Dec 25, 2018 at 14:16
  • Hi @KrzysiekDróżdż, even though most of those functions operate at a low level, caching it may still make a great difference for performance-critical applications. Let's take a popular plugin for instance, it can be used on websites with millions of users, many with PHP 5.*, imagine that improvement to all users! Other than that, I think that it's elegant as a programmer to minimize the computational effort if we can, the more complex the creation of the CPT, such as with many metaboxes, the better the cache will kick in. But you might be right. I would like to benchmark it if possible. – Lucas Bustamante Commented Dec 25, 2018 at 14:32
  • 1 you have to remember, that storing and getting data to/from cache also are operations and take some time ;) You won’t make any difference by reducing 10 basic PHP operations. – Krzysiek Dróżdż Commented Dec 25, 2018 at 14:36
  • Makes sense @KrzysiekDróżdż, specially if the PHP operations run on CPU/Mem and the cache on disk/storage – Lucas Bustamante Commented Dec 25, 2018 at 14:37
Add a comment  | 

1 Answer 1

Reset to default 0

You’re completely right at the beginning - caching is always a good idea, when you deal with results that are hard to get. But...

That’s not the case in here. Registering post types and meta boxes is not complex at all - it’s just few PHP operations. They won’t take much time (nor computation power) to be done.

So what is complex? Everything that takes long time, needs many DB queries or a lot of computation power/resources.

Let’s say you want to calculate number of words on your site. So you have to loop through all posts and count words. That’s perfect case for caching - it’s complex, because it’ll take much time - so you do it once and store the result in cache (probably in transients) for later.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749068932a316083.html

最新回复(0)