Object references
2 min read
Every read and write function that takes $object (wpmcf_get_field(), wpmcf_the_field(), wpmcf_has_field(), wpmcf_update_field(), wpmcf_delete_field(), wpmcf_check_password(), wpmcf_get_password()) accepts the same forms.
Accepted forms
You pass | Means | Context |
|---|---|---|
| the current object of the context |
|
positive | that ID |
|
| that post |
|
| that user |
|
| that term |
|
| that comment |
|
| post 12 |
|
| user 3 |
|
| term 5 |
|
| term 5 |
|
| comment 7 |
|
| widget 9 |
|
| the options-page row |
|
References are case-insensitive and trimmed (' User_3 ' works).
wpmcf_get_field( 'g', 'f' ); // current post
wpmcf_get_field( 'g', 'f', 42 ); // post 42
wpmcf_get_field( 'g', 'f', get_queried_object() ); // whatever the page is about, if it is a post/term/user
wpmcf_get_field( 'g', 'f', 'user_3' );
wpmcf_get_field( 'g', 'f', 'term_5' );
wpmcf_get_field( 'g', 'f', 'category_5' );
wpmcf_get_field( 'g', 'f', 'comment_7' );
wpmcf_get_field( 'g', 'f', 'option' );
wpmcf_get_field( 'g', 'f', 3, [ 'context' => 'user' ] ); // same as "user_3"The context argument wins
An explicit $args['context'] beats the context a reference implies:
wpmcf_get_field( 'g', 'f', 'user_3', [ 'context' => 'post' ] ); // reads POST 3, not user 3Use one or the other, not both.
Details worth knowing
- Taxonomy prefixes only choose the term context.
"category_5"and"post_tag_5"both mean "term 5 of whatever taxonomy it belongs to"; the taxonomy name is not compared with the term (verified:"post_tag_N"returned the value of a category term with that ID). The prefix must be a registered taxonomy, otherwise the reference is invalid. - Without an ID, only the post context has a "current" object.
context => user|term|commentwith no ID has nothing to read. The read returns thedefault; a write returnsfalse. - The object must exist to write (
wpmcf_update_field()): writing to'user_999999'or post99999999returnsfalseso no orphan rows are created. Reading a missing object returns thedefault. - Options need no ID.
"option"andcontext => optionsignore any ID. - Widgets need either
"widget_{id}"or the three argumentswidget_sidebar,widget_id_baseandwidget_numberwithcontext => widget(not exercised end to end in testing).
Invalid references
These are invalid: negative numbers, floats, true, arrays, objects of other classes, strings such as 'foo', 'user' or 'user_abc', and "{unknown}_5".
Call | Result |
|---|---|
a read ( | the |
a write |
|
a print ( | prints nothing |
with | a |
Nothing throws.
Getting an ID or reference from WordPress
$user = wp_get_current_user();
wpmcf_get_field( 'profile', 'bio', $user ); // WP_User works as is
wpmcf_get_field( 'profile', 'bio', 'user_' . get_the_author_meta( 'ID' ) );
if ( is_tax() || is_category() || is_tag() ) {
wpmcf_get_field( 'brand', 'logo', get_queried_object() ); // WP_Term
}
foreach ( get_comments( [ 'post_id' => get_the_ID() ] ) as $comment ) {
wpmcf_get_field( 'review', 'rating', $comment ); // WP_Comment
}Where each context's group comes from
A group only has a table row for objects its location rules include (see Field groups). A group that is attached to posts only never has user values. Reading an object type the group is not attached to returns the default.
Still stuck? Contact support