Web Development

WordPress Development

WordPress Custom Post Types and Advanced Custom Fields

March 12, 2015

Often times, we work with clients who need a products page or a team page that displays collections of dynamic content that include various types of data (text, images, charts, pdf downloads, etc.) per item. Sometimes we’ll even be asked to show various pieces of those items in multiple locations on the site – a menu of the collection, for example.

There’s often no one right way to do things. Though, when using WordPress, we’ve found a method of achieving this while getting ultimate customization and reusability, as well as an intuitive, fool-proof admin experience.

Let’s take the products page as an example. We recently had a client with a particular product suite in which a customer would choose a package that would always include a main component. The customer could then review, compare, and select a combination of multiple sub-components. Each sub component needed a description, image, and spec chart that would display on the page, ideally dynamically, in a responsive layout with an intuitive user experience that could also be easily managed in the back end.

Our method utilizes the indispensable plugin, Advanced Custom Fields, in combination with custom post types to create an easily managed, easily queried set of posts that can contain any type of data needed. Let’s look at how to quickly set up something similar.

Install the Plugin

First, and perhaps it goes without saying, we’ll need a WordPress install. Second, if you install no other plugin, install Advanced Custom Fields Version 4.4 is what is available on the WordPress Plugin Directory at the time of this writing, but we happen to know that if you want to go for a paid pro account, Version 5 has some pretty killer features. For our purposes here, the free version is all we’ll need.

Register Your Custom Post Type

The next step deals with WordPress’ standard custom post type registration. Here, we’ll register the Products post type. For more information on registering custom post types, parameters, and custom taxonomies, see Add this to your functions.php file.

Set Up Advanced Custom Fields

Now that we have our newly created custom post type, we can start to create all of the products we want as individual posts. But what if we want each product to include a title, main description, a downloadable pdf of specifications, and two images? We could cram all of that in the one wysiwyg WordPress gives us and hope for the best, but that’s not going to be the most reliable and easily managed method of building our back end. This project sounds like we need some custom fields. First, head to the Custom Fields admin page and add a new field group. Call this group ‘Product Info’ and add a new rule in the Location box that reads ‘Show this field group if Post Type is equal to Product’. If you do not see Product as an option to select, go back and make sure you registered your custom post type correctly. rule Next, start adding fields to the group. This tutorial assumes some level of familiarity with the plugin, but if you aren’t familiar with Advanced Custom Fields, have a look at their documentation: Because each product post will already have a title, a content wysiwyg, and a featured image, we can use those as product title, product description, and one of the images.  For the other product features, add a File field called ‘Download’ with a return value of File Array, and an Image field called ‘Product Image’ with a return value of Image URL. product_info

Querying the Products

After adding a few products, we need to output them somewhere. This can be done in whichever way works best for your site. You can make a template php page that queries the posts, or use a custom shortcode; regardless, you are now in a position use any part of the products data in any way you wish. Here is an example of a basic query:

This is a very simple query that will output all of the products entered, but you can see that any of these elements can be queried on any page. Everything is managed on the backend in one place and the opportunities for reusing the data are endless. One query can be used to create a menu of products as well as each product output. A product that relates to a particular blog article can be queried on that article’s single view page. If you are into the new WordPress api, this will be exposed for use there as well.

If your WordPress site needs more flexible data with an easy-to-manage back end, Advanced Custom Fields is the perfect solution for visually creating fields, selecting multiple input types, assigning fields to multiple edit pages, easily loading data through a clear and familiar interface, and improving upon native WordPress custom post type and metadata features for ease and fast processing. If you need help building, customizing, or refreshing your web presence, Fresh can help. Contact us for more information or a free quote.

Looking for more on WordPress? Check out our other WordPress posts.

Dave Reese

Sr. Front-End Developer

  • This is the first CPT and ACF tutorial I’ve come across that is easy to follow and explains exactly what is happening. Was able to follow this on my VPS environment and got it to work perfectly.

    Thank you very much for this and I look forward to expanding on this for my projects.

    I do have one question. How do you add a thumbnail when using this setup? More specifically, this code:

    $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), ‘full’ );

    I can’t figure out where this is referenced in the custom post type when adding a Product.

  • Thank you very much for this article. It taught me how to make the custom posts, I feel quite powerful now! 🙂

  • Thank you, so simple and genius! 🙂

  • Jack Davies

    Thank you! I’ve been looking for the past hour on how to serve up my custom toolset types images in a 1:1 ratio. This was the perfect solution!

  • Awesome, thank you so much.

  • Great article. How do you recommend using ACF to add a WYSIWYG editor to the primary CPT menu, which would display on the archive page for the CPT? Thanks!

  • mobby

    I had created a custom field as Specialty under field group Doctor.
    When I do get_fields() it just returns false. What wrong I am doing? thank you

  • Ahmed Nour El Din

    variables are always empty , don’t know why

You might also like...



9 Steps for Moving Sites Between Servers

Technical needs change over time. To have a better optimized website, this may mean migrating your website to a different server. Perhaps you’ve found a server more conducive to efficiency and performance, and you need guidance about how to switch. While it may seem like more hassle than it’s worth, moving your site isn’t as … Continued



Dev Terminology 101: Common Programming Languages (and their key frameworks)

Language enables human communication – spoken or written – with words and phrases structured in a conventional way. Just like we need different languages to communicate with one another, we also need different languages to communicate with computers. In an article on Stack Overflow, Matt Sherman writes that at a foundational level, all programming languages … Continued



Announcing Fresh Consulting’s WCAG 2.0 PHPCS Linter

Today, Fresh Consulting is announcing the release of our WCAG 2.0 PHPCS Linter. The linter is a set of rules (or sniffs) for PHP Code Sniffer which can automatically detect and alert when certain WCAG 2.0 violations are detected in code. WCAG 2.0 supports designers and developers in meeting the guidelines and success criteria of accessibility. The … Continued

Pacific Science Center

application maintenance and development

Pacific Science Center


mobile development



web design, microsite development


Seattle Public Library

ui/ux design, microsite development

Seattle Public Library