WordPress.org

Forums

Is register_post_type called only in admin? (5 posts)

  1. RichardMisencik
    Member
    Posted 1 year ago #

    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

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. RichardMisencik
    Member
    Posted 1 year ago #

    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?

  4. bcworkz
    Member
    Posted 1 year ago #

    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.

  5. RichardMisencik
    Member
    Posted 1 year ago #

    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');
    }

Topic Closed

This topic has been closed to new replies.

About this Topic