Autoload custom post types

I’m trying to autoload my custom post types. I think I got this working before but I must be missing something.

In a sage9 theme, I have a file app/post-types/story.php :

// app/post-types/story.php
namespace App;
use PostTypes\PostType;

$stories = new PostType('story');
$stories->register();

Now, when I load this file from the resources/functions.php file, the post type shows up in the WP admin menu.

// resources/functions.php
array_map(function ($file) use ($sage_error) {
    // ...
}, ['helpers', 'setup', 'filters', 'admin', 'post-types/story']);

I’d prefer to auto-load the custom post types, instead of changing the functions.php file. I thought I got that working before by adding this to app/setup.php:

// app/setup.php
add_action('init', function () {
    collect(glob(config('theme.dir') . '/app/post-types/*.php'))->map(function ($cpt) {
        return require_once($cpt);
    });
});

Instead, it doesn’t show up in the WP admin menu. The custom post type file is loaded though. Adding echo 'hello'; to it, displays ‘hello’ on all pages including the WP admin.

This topic was automatically closed after 42 days. New replies are no longer allowed.