Model Usage - 10quality/wpmvc-addon-metaboxer GitHub Wiki

The model can be used as a regular Post Model.

Get a field value

To get the value of a saved field, simply use the field ID as a model property.

For example, if the model has defined the following field ID (slogan):

class Post extends Model
{
    // Class properties and methods...

    protected function init()
    {
        $this->metaboxes = [
            'metabox_id' => [
                'tabs' => [
                    'tab_id' => [
                        'fields' => [

                            'slogan' => [
                                'title' => __( 'Post slogan', 'my-domain' ),
                            ],

                        ],
                    ],
                ],
            ],
        ];
    }
}

The value can be retrieved as property, like this:

$post = Post:find( $post_id );
$slogan = $post->slogan;

Aliases

The model can still use aliases on top of the metaboxer fields, for example:

class Post extends Model
{
    /**
     * Aliases.
     * Mapped against custom fields functions.
     * @var array
     */
    protected $aliases = [
        'formatted_slogan' => 'func_get_formatted_slogan',
    ];

    // Class properties...
    protected function init()
    {
        // Other metaboxes...
    }

    protected function get_formatted_slogan()
    {
        return $this->slogan
            ? apply_filters( 'post_slogan', trim( $this->slogan ), $this->ID ),
            : null;
    }
}

Save model

The metaboxer model can be saved outside of the auto-generated metaboxes using the method save(), use this with discretion as this option will not apply sanitization on the data.

$post = Post:find( $post_id );

$post->slogan = 'New slogan';
$post->save();