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?