Register Custom Corcel Models for WordPress Post Types

📖 1 minute read

By default, Corcel uses a generic Post model for all WordPress content. But if you’re working with custom post types (recipes, portfolios, reviews, etc.), you’ll want dedicated models with proper type-hinting and scopes.

Register your custom models in config/corcel.php:

'post_types' => [
    'recipe' => App\Models\Recipe::class,
    'review' => App\Models\Review::class,
    'portfolio' => App\Models\Portfolio::class,
],

Then create your model:

namespace App\Models;

use Corcel\Model\Post;

class Recipe extends Post
{
    protected $postType = 'recipe';
    
    // Now you can add recipe-specific methods
    public function scopeVegetarian($query)
    {
        return $query->hasMeta('dietary_type', 'vegetarian');
    }
}

Now queries return your custom model instead of generic Posts:

// Returns App\Models\Recipe instances
$recipes = Recipe::vegetarian()->get();

// Auto-sets post_type when creating
$recipe = Recipe::create([
    'post_title' => 'Pasta Carbonara',
    'post_status' => 'publish',
]);

No more checking post_type manually—your models are now type-specific.

Daryle De Silva

VP of Technology

11+ years building and scaling web applications. Writing about what I learn in the trenches.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *