The plugin Advanced Custom Fields provides a simple and visual interface for creating metaboxes. The result is very intuitive and easy for the client to edit. The ACF plugin includes several functions that allow you to use your own template to pull the values of those fields in the metaboxes you created, but I recommend not using them.
For certain fields, such as images, using ACF functions will create additional queries to the database. In contrast, using core WordPress functions can significantly reduce the number of queries to the database.
You can check the number of queries performed on the database with the Query Monitor plugin.
Fetching Information Using get_post_meta Instead of get_field
There is no reason to use the get_field
function when you can use the WordPress function called get_post_meta
.
Besides the fact that ACF functions lead to many more database calls, if, for any reason, the plugin is removed or inactive, visitors to the site will see an error and a blank page instead of your site.
If you go through the trouble of wrapping all ACF functions in function_exists
, the site will load, but visitors won’t see the metadata you entered into ACF, even though it still exists in the database.
On the other hand, if you use core WordPress functions, visitors won’t experience any changes to your site even if the plugin is inactive.
Of course, in this case, the metaboxes won’t appear in the WordPress admin interface, and there won’t be an option to edit them.
However, this is surely less severe than a site that doesn’t load at all. Hence, using ACF functions leads to a dependency on the frontend or what visitors to your WordPress site see.
This guide provides several examples of using get_post_meta
to retrieve information instead of using functions that come with ACF.
The method I’m talking about can be straightforward for standard ACF fields but may be slightly less clear for slightly more complex ACF fields.
Using get_post_meta for Repeater Field
Let’s say you allowed the client to add videos as they wish using the ACF Repeater Field, and the fields include a title, video URL, and image.
Calling the meta field of the repeater field in your template with the following code will return the number of items in this field:
get_post_meta( get_the_ID(), 'my_videos', true );
You can loop through each of the existing items (videos) and access their values. Here’s how I display the video without using the get_field
function of ACF:
$videos = get_post_meta( get_the_ID(), 'my_videos', true );
if( $videos ) {
for( $i = 0; $i < $videos; $i++ ) {
$title = esc_html( get_post_meta( get_the_ID(), 'my_videos_' . $i . '_title', true ) );
$video = esc_url( get_post_meta( get_the_ID(), 'my_videos_' . $i . '_video', true ) );
$thumbnail = (int) get_post_meta( get_the_ID(), 'my_videos_' . $i . '_thumbnail', true );
// Thumbnail field returns image ID, so grab image. If none provided, use default image
$thumbnail = $thumbnail ? wp_get_attachment_image( $thumbnail, 'medium' ) : '<img src="' . get_stylesheet_directory_uri() . '/images/default-video.png" />';
// Displayed in two columns, so using column classes
$class = 0 == $i || 0 == $i % 2 ? 'one-half first' : 'one-half';
// Build the video box
echo '<div class="' . $class . '"><a href="' . $video . '">' . $thumbnail . '</a>' . $title . '</div>';
}
}
In the code above, you can see how to use the ACF Image Field. The identifier (ID) of the image is stored in the database, and you can actually get the markup of the image using:
wp_get_attachment_image( $image_id, 'image_size' );
Fetching Data from ACF Options Page
When you add metaboxes to the ACF options page, the information will be stored in the options table, not as post meta.
In this case, the meta_key you chose will be prefixed with options_
. If you have three fields on your ACF options page, you can access those fields as follows:
$title = esc_html( get_option( 'options_title' ) );
$button_text = esc_html( get_option( 'options_button_text' ) );
$button_url = esc_url( get_option( 'options_button_url' ) );
if( $title && $button_text && $button_url )
echo '<div class="call-to-action"><div class="wrap"><p>' . $title . '</p><p><a href="' . $button_url . '" class="button">' . $button_text . '</a></p></div></div>';
Term Metadata
Assuming you added a field called “Secondary Title” to categories, ACF will also store this in the options table in the following format: [taxonomy]_[term_id]_[field name]
To retrieve the value of the field called my_subtitle on the archive page of categories without using ACF functions, you can use the following code:
// Make sure this is a category archive
if( ! is_category() )
return;
$category = get_term_by( 'slug', get_query_var( 'category_name' ), 'category' );
$subtitle = esc_html( get_option( 'category_' . $category->term_id . '_my_subtitle' ) );
if( $subtitle )
echo '<p class="subtitle">' . $subtitle . '</p>';
That’s it! 🙂
Thanks for this awesome tips. I use ACF a lot in my designs so this helps a lot. I have one question with Repeater. What if the repeater is in the ACF options page, how would I access it using the native WordPress functions.
Do I call it say
$social_links = get_option( ‘options_social_links’ ) ;
Thanks
PC
Hi Pepin,
Yes, Thats how you call it. You will obviously need to loop through the values of the repeater field after. So if you have a field named txt in the repeater field you can display it like that: