Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model Issue #3

Open
classicalrehan opened this issue Jul 27, 2016 · 1 comment
Open

Model Issue #3

classicalrehan opened this issue Jul 27, 2016 · 1 comment

Comments

@classicalrehan
Copy link

Hello,

is there any function getting all post meta, as I using find($id) this is given me single post based on id. please help me for this

Thanks
Rihan

@amostajo
Copy link
Owner

amostajo commented Jul 28, 2016

There are 2 way to retrieve the desired meta.

1) By accessing the meta array property

// Access a specific key
$post->meta['key'];

// PRINT ALL META
print_t($post->meta);

2) By defining aliases

If you have a meta key called, for example, post_count (this is the exact name of the key in postmeta table).

The you can define the alias for it in your model like:

class Post extends Model
{
    protected $aliases = [
        'count' => 'meta_post_count'
    ];
}

And later use it like:

$post->count;

NOTE: Wordpress protected meta with prefix _wp_ need to be defined as aliases or else they will not be loaded by model.

Example:

class Post extends Model
{
    protected $aliases = [
        'wp_value' => 'meta__wp_post_value'
    ];
}

NOTE: For scope and security, only meta defined as alias can be updated into the DB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants