A Cozmoslabs Product
Documentation / Custom Fields Creator / How to / Retrieve a Custom Field as a Variable

Retrieve a Custom Field as a Variable

To assign the Custom Field value to a Variable we use the function get_cfc_field().

For a Single Meta Box

In The Loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 
 * Let's say we want to retrieve the Upload Field Image (the slug will be image) from a Single Meta Box  
 */
 
<?php $img = get_cfc_field('my_meta_name', 'image'); ?>
/* 
 * For a image the attachment object looks like this:
   array(
	'id' => 151,
	'alt' => 'Avatar Image',
	'title' => 'John',
	'caption' => 'Our number on member',
	'description' => 'a avatar image',
	'url' => "http://www.yourdomain.com/wordpress/wp-content/uploads/2013/10/John.jpg",
	'width' => "100",
	'height' => "120",
	'sizes' => Array
        (
            [thumbnail] => "http://www.yourdomain.com/wordpress/wp-content/uploads/2016/10/John-150x150.jpg",
            [medium] => "http://www.yourdomain.com/wordpress/wp-content/uploads/2016/10/John-300x119.jpg",
            [large] => "http://www.yourdomain.com/wordpress/wp-content/uploads/2016/10/John.jpg"
        )
);
*/

Outside The Loop:

1
2
3
4
5
6
/*
 * Let's say we want to get the field Bio (the slug will be bio) 
 * From a Single Meta Box from the post with the id 153
 */
 
<?php $bio = get_cfc_field('my_meta_name', 'bio', 153 ); ?>

For a Repeater Meta Box

For the Repeater Meta Box we also need the function get_cfc_meta().

In The Loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* 
 * Let's say we want to get all the fields Author (the slug will be author) of the Field Type User Select
 * From a Repeater Meta Box  
 */
 
<?php foreach( get_cfc_meta( 'my_meta_name' ) as $key => $value ){ ?>
    <?php $author = get_cfc_field( 'my_meta_name','user', false, $key ); ?>
<?php }  ?>
 
/*
 * $author contains the following:
array(11) {
  ["ID"] => "33"
  ["user_firstname"] => "John"
  ["user_lastname"] => "Smith"
  ["nickname"] => "J.Smith"
  ["user_nicename"] => "johnsmith"
  ["display_name"] => "John Smith"
  ["user_email"] => "jsmith@mail.com"
  ["user_url"] => "www.johnswebsite.com"
  ["user_registered"] => "2012-08-16 09:58:36"
  ["user_description"] => "Biology Teacher"
  ["user_avatar"] => "<img width="96" height="96" class="avatar avatar-96 photo" src="http://0.gravatar.com/avatar/avatar.jpg" alt="">"
}
*/

Outside The Loop:

1
2
3
4
5
6
7
8
/*
 * Let's say we want to get all the fields Date (the slug will be date) 
 * From a Repeater Meta Box from the post with the id 252 
 */
 
<?php foreach( get_cfc_meta( 'my_meta_name', 252 ) as $key => $value ){ ?>
   <?php $date = get_cfc_field( 'my_meta_name','date', 252, $key ); ?>
<?php }  ?>

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket