v2 Documentation
Queries / Results Data
When a query returns results they are wrapped in an object that you can work with just as you would any other WordPress result's object. While WordPress provides the option to return either an object or an array, WPM will return only objects so this additional data can be accessible.
Ancestor
->ancestor()
Use ancestor
to get either the parent of the current result or all ancestors.
// get a page
$page = wpm('q.page')->find(746);
// get the immediate parent of the current result
$parent_page = $page->ancestor()->first();
// get all ancestor ids of the current page
$parent_pages = $page->ancestor()->get();
Children
->children()
Use children
to get either the first child of the current result or all children.
// get a page with children
$page = wpm('q.page')->find(174);
// get the first child of the current result
$child = $page->children()->first();
// get all children of the current result
$children = $page->children()->get();
Meta
->meta()
Meta data adds key/value pairs to a post. With meta
you can access those pairs with ease.
// get the first member
$member = wpm('q.member')->first();
// return a single meta value for the "employee_id" key
$employee_id = $member->meta('employee_id')->first();
// return an array of meta values for the "teams" key
$teams = $member->meta('teams')->get();
// return all meta data for the member
$meta = $member->meta()->all();
Tag
->tag()
When a post has a taxonomy, you can access the associated taxonomy terms by using term
.
// get a post
$post = wpm('q.post')->find(1178);
// get all tags
$tags = $post->tag()->all();
// create a list of tags
// arguments for list are before, separator, after
echo 'Tags: ' . $post->tag()->lists(null, ',');
// create an unordered list from the tags
echo $post->tag()->lists('<ul><li>', '</li><li>', '</li></ul>');
Term
->term()
When a post has a taxonomy, you can access the associated taxonomy terms by using term
.
// get the first member
$member = wpm('q.member')->first();
// get all terms from the "skill" taxonomy
$skills = $member->term('skill')->get();
// get first term from the "skill" taxonomy
$skills = $member->term('skill')->first();
// create a list of terms from the "skill" taxonomy
// arguments for list are before, separator, after
echo 'Skills: ' . $member->term('skill')->lists(null, ',');
// create an unordered list from the terms
echo $member->term('skill')->lists('<ul><li>', '</li><li>', '</li></ul>');
// get all terms grouped by taxonomy
$tax_terms = $member->term()->all();