I would like to create a post programmatically, save the following:
title
content
category (this where it’s more complex than simply creating an eloquent record)
Have looked through the docs but doesn’t seem to be anything on it … any help much appreciated!
Hi @trader47 ,
this is a CMS page code to create a new post. Modify it as you like or you can create separate plugin if you wish
title = "Blog new article"
url = "/blog/new"
is_hidden = 1
==
<?php
use RainLab\Blog\Models\Post as BlogPost;
use RainLab\Blog\Models\Category as BlogCategory;
function onStart()
{
$blogCategory = BlogCategory::where('slug', 'webdesign')->first();
$blogPost = new BlogPost();
$blogPost->title = 'New blog post';
$blogPost->slug = 'new-blog-post';
$blogPost->content = 'This is a new blog post';
$blogPost->published_at = new \DateTime();
$blogPost->published = true;
$blogPost->categories()->add($blogCategory);
$blogPost->save();
}
?>
==
PS: In $blogCategory
I search for a category with a slug webdesign
- change it to whatever you need.