Support » Plugins and Hacks » Hacks » Is register_post_type called only in admin?

  • Is register_post_type function only called in admin?

    I have several custom post types, each in a separate file like cpt-books.php, cpt-movies.php etc. Besides just register_post_type I have code to add metaboxes etc. for each post type.

    Then in functions.php I’m requiring these files like so:

    require_once(‘cpt-books.php’);
    require_once(‘cpt-movies.php’);

    Can I do it this way? So these files are only loaded in the admin and not on the front end?

    if(is_admin()) {
    require_once(‘cpt-books.php’);
    require_once(‘cpt-movies.php’);

    }

    Is it going to work properly? Or

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to register for all requests, front and back ends. If only done for admin, no one would be able to view the posts, only administrate them.

    Requiring once in functions.php is fine, but the actual register_post_type() call does need to be from within a hook callback to the ‘init’ action.

    Thanks for the answer, yea I use register_post_type() inside a function and then call it on init.

    So the actual hook is doing its own thing so it doesnt register the post on every page load right?

    Moderator bcworkz

    (@bcworkz)

    Wishful thinking I’m afraid. It does register on every page load! It’s truly staggering to me how much code is run for any given page. If there is some condition you can check at the init action to decide unequivocally whether the post type is needed or not, it would certainly be worth doing.

    I think its supposted to be that way, and its expecting it to be that way, because even when I register the post type, and then comment out the code. It removes the post type.

    Same with using:

    if(!post_type_exists('post-type')) {
        add_action('init', 'my_register_post_type');
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Is register_post_type called only in admin?’ is closed to new replies.