Advanced Custom Fields: Harnessing Custom Post Types

Today’s guest blog post is part 2 of 2 in a series written by Isaac Castillo. Isaac is the founder of Echo Design Solutions, a San Antonio-based WordPress and WooCommerce development company. We recommend that you review part 1 of the series before diving in to part 2.


acf

In the first part of our series on Advanced Custom Fields, we explored how Advanced Custom Fields and custom post types can be used to add virtually any functionality to the admin area of a WordPress site. By following the real-world examples from our first post, you can enhance the administration of your site. This is a great first step, but it stops short of displaying content from a custom post type on your site.

In this second part of our series on Advanced Custom Fields, I’ll help you cross the finish line by demonstrating one of the easiest ways to pull data from a custom post type and display it on your WordPress site. While there are numerous ways to pull data from custom post types, the method that I’ll demonstrate in this post has become my preferred choice for its simplicity.

Before we begin, we’ll make a quick change to our custom post type:

‘has_archive’ => true,

With our custom post updated, we need to create a file named “archive-company_team.php” inside of the site’s theme folder. In the example I provided in the first part of our series, the custom post type was named “company_team”. The format for any such file is “archive-{post type name}.php”, with “post type name” being the name given to your custom post type.

Next, we’ll pull records from the custom post type in a usable way. 

<?php

/**

* The template for displaying pages

*

* This is the template that displays all pages by default.

* Please note that this is the WordPress construct of pages and that

* other “pages” on your WordPress site will use a different template.

*

* @package WordPress

* @subpackage Echods

* @since Echods 1.0

*/

 

get_header(); ?>

 

<?php if ( have_posts() ) : ?>

           <?php while ( have_posts() ) : the_post(); ?>

<?php echo the_title(); ?>

 

<?php echo the_content(); ?>

<?php endwhile; ?>

<?php endif; ?>

 

<?php get_footer(); ?>

The code in the example above will check to see if there are any posts. Because the name of the file matches WordPress’ template hierarchy, this code will work as it should right out of the box. For the sake of clarity, I’ve stuck with the important information in this example. Keep in mind that, on your site, your code will be wrapped with your site’s html.

“But Isaac,” I hear you asking, “how do we pull that information out of our custom post type?” I am so glad you asked. The Loop provides us with the mechanism we need to access data from within a custom post type. All we need to do is add the following code:

the_field(‘field_name’);

As long as the code with within The Loop, we won’t need to pass any additional information. This makes it as easy as pie to create any customization we need from our previously-created custom post type. And as long you stick to WordPress’ naming convention, the code in our example will always work as expected.

But what can you do when you needed to display data from a custom post in a different location, such as your site’s home page? To tackle this challenge, I like to add queries to the plugin’s functions.php file:

<?php

 

/**

* Get all team query

*

* @return object

*/

function get_team_members() {

 $args = [

   ‘post_type’ => ‘company_team’,

   ‘posts_per_page’ => -1,

 ];

 

 return new WP_Query( $args );

}

Next, on the site’s home page or in the template file where I need to use this data, I’ll call a function and create my own loop from the function:

$query = get_team_members()

 

<?php if ( $query->have_posts() ) : ?>

           <?php while ( $query->have_posts() ) : $query->the_post(); ?>

<?php echo the_title(); ?>

 

<?php echo the_content(); ?>

<?php endwhile; ?>

<?php endif; ?>

 

<?php wp_reset_postdata(); ?>

This approach can be used any time you need to display data from a custom post type in multiple locations. Note that, in the example above, I reset the post data with a function call after the loop. This was done to prevent WordPress from getting confused when it comes to ordinary posts.

Custom post types are my go-to tool for making practically endless customizations to WordPress sites. I use custom post types with Advanced Custom Fields any time a site’s content requires more than a title and description, and I recommend that you do the same. Happy coding!


4 Simple SEO Tips for New WordPress Site Owners

Developers’ Picks WordPress makes it easy to boost your site’s traffic by optimizing your site for search engines (SEO). If you recently launched a site on WordPress, it’s important to know which SEO strategies you should implement first, and which ones you can delay or do without. We reached out four of our agency partners for their advice, and asked them, “what’s the best SEO tip you can give to a new WordPress site owner?” Their responses were filled with great insights, so we decided to share their tips with our

Read More


Pressable Recognized for Top-Tier Performance in WordPress Hosting Benchmarks

Pressable, a leading provider of managed WordPress hosting, was recently recognized as a “Top Tier” performer in the 2016 WordPress Hosting Performance Benchmarks by Review Signal. Now in its fourth year, the Review Signal WordPress Hosting Performance Benchmarks is the industry’s leading guide to high-performance WordPress hosting. The “Top Tier” is Review Signal’s highest designation, and is awarded to hosts who reliably deliver webpages in under 1 second with over 99.9% uptime. Pressable achieved “Top Tier” status for its performance

Read More


Advanced Custom Fields: Custom Post Types

Today’s guest blog post is part 1 of 2 in a series written by Isaac Castillo. Isaac is the founder of Echo Design Solutions, a San Antonio-based WordPress and WooCommerce development company. As a WordPress developer, I’m often tasked with solving uncommon problems. I lean heavily on WordPress’ extensive library of plugins to help me achieve those results. As of today, there are 46,483 registered plugins available on the WordPress Plugin Repository. Those plugins have been downloaded a total of 1,387,239,989

Read More


3 Must-have Extensions for WooCommerce

WooCommerce is by far the most popular eCommerce plugin for WordPress. Packing an impressive suite of built-in features, WooCommerce makes it easy to set up an online storefront on an existing WordPress website. For site owners and developers who want to get the most from WooCommerce, there are nearly 400 extensions available that can further enhance its functionality. In this post, we’ll explore three WooCommerce extensions that you should consider installing on your online store. 1. WooCommerce Subscriptions Do you

Read More