1

A while ago I was following a Tutsplus video tutorial on creating custom post type, and so accordingly I made 2 similar small custom post types for myself.

Today I was exploring ways on how can I optimize my website, so I installed a plugin (Query Monitor) which shows how many sql queries are made on every page. I was surprised to see that on every page (frontpage.php, single.php, etc.) I have more than 200 sql queries :o Even on attachment page where there is only one image! When I disable plugins, there are about 60 sql queries.

class branje_biljaka {

    public  $months = array('...');

    public  $terms = array('...');


    public function __construct()
    {
        $this->register_post_type();
        $this->register_months();
        $this->add_meta_box();
    }


    public function register_post_type()
    {
        $args = array(
            'labels' => array(
                'name' => 'Kalendar branja',    
                ),
            'query_var' => 'branje',
            'rewrite' => array(
                'slug' => 'kalendar-branja'
                ),
            'public' => true,
            'publicly_queryable' => true,
            'has_archive' => true,
            'menu_position' => 5,
            'hierarchical' => false,
            'exclude_from_search' => false,
            'supports' => array('title', 'excerpt')
            );
        register_post_type('branje_biljaka', $args);
    }

    public function register_months()
    {


        foreach($this->months as $month => $month_var)
        {
            $args = array(
                'labels' => array('name' => $month),
                'hierarchical' => true,
                'sort' => true,
                'query_var' => $month_var
                );
            register_taxonomy($month_var, 'branje_biljaka', $args);
            $this->registerTerms($month_var);
        }

    }


    public function registerTerms($taxonomy)
    {

        foreach($this->terms as $term)
        {
            wp_insert_term($term, $taxonomy);
        }

    }



}

    add_action('init', function(){
        new my_custom_post_type();
    });

I only need to show data on custom post type pages (archive-ctp.php, single-ctp.php), and I would guess the problem is with add_action('init', function()); That is what I see in Codex too https://codex.wordpress.org/Function_Reference/register_post_type How do I restrict registering and everything about this post type for only when it is needed?

1 Answer 1

1

Custom post-type and taxonomy definitions are not saved to the database, and so registering them only makes WordPress "aware" of their existence for that page request. As custom post-type/taxonomy registration does not require database interaction, the performance impact is negligible. I've never actually seen any applications that selectively register CPTs/taxonomies (though I'm sure they're out there).

Inspecting the problematic queries themselves should give you a good idea of what's causing them. In this instance, they will likely point to your use of wp_insert_term(), which attempts to insert all of those terms into the database on every page load. You should consider doing this one time only when your plugin is first activated by using an activation hook.

1
  • omg it was that easy.. 150 less queries for each of 700 posts and it was like that for a whole year :D Thank you :) Commented Dec 14, 2015 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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