Closed Hyra closed 8 years ago
Are you talking about validation? Or how to add multiple custom fields to your custom post type?
There is a thread talking about building a model class in order to construct a custom post type and retrieve its data but it's not yet in the pipeline. For next version, we still need to work with the current API.
No not validation, just the "setting things up" part. No idea if you're familiar with Pods.io but with it you can define custom post types by adding fields and the types.
Ideally one would want this done through modeling in a custom model PHP file, but couldn't see any implementation.
Can you point me to the thread you mentioned?
In order to attach custom fields to a custom post type, you first need to create a metabox in which to add/display the custom fields.
For example, let's say you have a custom post type with a name/slug of books
, you'll register custom fields like so:
$books = PostType::make('books', 'Books', 'Book')->set(['public' => true]);
$fields = Metabox::make('Info', $books->get('name'))->set([
// Book's author - Text field
Field::text('book_author', ['title' => 'Author']),
// Book's images - Collection field
Field::collection('book_gallery'),
// Book's excerpt - Textarea field
Field::textarea('book_excerpt'),
// Add more fields...
]);
You can also "sanitize/validate" your fields values like so:
$fields->validate([
'book_author' => ['textfield', 'min:3']
]);
Regarding the thread, it's about adding a WP-CLI package where I mention the idea of a model class to build custom post type. Here is the thread, if you have any suggestions. don't hesitate to drop a line in it:https://github.com/themosis/framework/issues/296
Oh wow, that's pretty darn perfect. Thanks for the pointer, sir.
Much appreciated!
👍
Maybe one little extra question as I couldn't deduct it from the Metabox nor Taxonomy page. Does Themosis provide an option to have a relationship associated to a model?
For instance, books hasOne post
$fields = Metabox::make('Info', $books->get('name'))->set([
Field::relation('Associated Post', [
'model' => 'posts',
'field' => 'some_title_field'
])
]);
I did see you can sort of do this with the Taxonomy field, but it would be kind of nice to have a dropdown in that same Metabox where you can select something.
There is no such field but for a hasOne
relation, you can easily do it with a select
field where you put a list of posts as options and each option has the post ID
as a value.
On a quick example:
$options = [];
foreach(PostModel::all() as $post) {
$options[$post->ID] = $post->post_title;
}
Metabox::make('Info', $books->get('name'))->set([
Field::select('associated-post', [$options], ['title' => 'Associated Post'])
]);
The field will then register the post ID
as a meta_value
that can be re-used in your queries.
.. brilliant
Great work on this, very awesome.
How would you guys suggest working with Custom Post Types that require multiple custom fields? Creating a proper Model for it would make the most sense, but this doesn't seem to support custom fields.
I was considering going with the "traditional" Pods plugin and see if the Model can still read from these, but maybe there's better practices in place already.
Thanks!