Custom Fields
Documentation for version 2.1 or later
Regular meta
The Custom Fields extension supports meta of three object types:
Posts
Users
Comments
Here are the examples with equivalents in get_****_meta()
Note that {post_ID}
or {user_ID}
or {comment_ID}
part is the object ID which can be static (like just 365783
) or can be a merge tag. If you are using merge tags for this part, like in the examples, please make sure you can see this merge tag in the sidebar. So if you choose the Book updated trigger, the {post_ID}
will become {book_ID}
.
Advanced Custom Fields meta
Any ACF field value can be retrieved with the regular meta merge tags, but sometimes you'd want to let ACF format the data for you.
Very similar to regular meta you can use it like this:
Which is an equivalent of:
You can access the first-level array key as well
Giving an example, you can have a field of type User and return format of User array. Your meta will look like this:
To access the user_email
key, you just need to do it like this:
BuddyPress XProfile
BuddyPress saves XProfile fields outside the user meta database tables, so it cannot be downloaded with acf
or usermeta
Merge Tags.
Instead, there's another type of xprofile
Merge Tag, see the below examples.
Using nice field name
Using field ID
Output modifiers
Access array keys
Getting the meta often means you need to work with arrays. Deeper levels can be easily accessed with :
character.
Let's consider an example, where you have this array in your meta:
To access the email
key you just need to pass the array key after the colon:
This will work with any type of meta.
Giving the ACF example, you can have a field of type User and return format of User array. Your meta will look like this:
To access the user_email
key, you just need to do it like this:
Multidimensional arrays
But what in case you have a multidimensional array like this?
Easy! Just pass the nested keys after another colon:
Same story with numeric arrays:
Pipelines
Sometimes the value in meta is not exactly in a form you'd like. The most common scenario is having a post or user ID saved in the meta while you want to display a post_title
or display_name
.
Each pipe is processing the value and manipulate its representation. You can think about it like this:
So the initial value of User ID can be replaced with a whole User object and this object can be formatted as JSON.
Pipes can be mixed together and each pipe can be used with the array accessors. They work with ACF and regular meta Merge Tags. See the example.
All supported pipelines:
Objects
Manipulators
Formatters
post
Gets the post object as an array.
postmeta
Gets the post meta.
$key
and $single
arguments are optional. If you omit the $key
argument all the post meta is retrieved. This is an equivalent of get_post_meta
function.
$single
will evaluate to boolean, so you can write any of: on
, true
, 1
, yes
, etc.
term
Gets the term object as an array.
termmeta
Gets the term meta.
$key
and $single
arguments are optional. If you omit the $key
argument all the term meta is retrieved. This is an equivalent of get_term_meta
function.
$single
will evaluate to boolean, so you can write any of: on
, true
, 1
, yes
, etc.
user
Get the user object as an array.
usermeta
Gets the user meta.
$key
and $single
arguments are optional. If you omit the $key
argument all the user meta is retrieved. This is an equivalent of get_term_meta
function.
$single
will evaluate to boolean, so you can write any of: on
, true
, 1
, yes
, etc.
pluck
Plucks the values from the collection
$key
argument is required. This is an equivalent of wp_list_pluck
function.
Plucking the name
key from this array:
Will result with:
join
Joins the array values into a comma-separated string.
Joining an array:
Will result with:
first
Gets the first element of an array.
Getting the first element of an array:
Will result with:
last
Gets the last element of an array.
Getting the last element of an array:
Will result with:
bool
Formats the boolean value in the desired way.
$true
String to return when the value is evaluated totrue
$false
String to return when the value is evaluated tofalse
Example:
json
Formats the value as json.
Value formatting
String values are always displayed as literal strings. No HTML is escaped.
If the returned value is an array with a single item, it's automatically unwrapped and the first item is returned.
Array values are represented with print_r()
function and wrapped with <pre>
tags. Example:
Boolean values are represented as Yes
or No
by default.
You can change the format by using the formatting pipes, like bool, join, or json.
Examples
Post has a related_post
field (stored post ID) where another related post can be selected. The related post has a field where owner
can be selected and this returns the user ID. To get this user email:
Explanation:
{postmeta {post_ID} related_post
- gets the initial value of the related post, post ID is retrieved|postmeta,owner,true
- related post ID is used to get the meta keyowner
andtrue
means the single value should be returned. We get the User ID.|user:user_email
- user ID is used to get the whole user representation. We access theuser_email
array key to get the email
Limitations
The plugin cannot handle iterators. Ie. if you want to map an array of users. In other words, it's not possible to work on the collections. The subsequent pipes are meant to reduce the data to a single value.
Last updated