WordPress Development for Noobs is for people who are trying to develop WordPress websites for the first time. The process will make more sense if you’ve done some front-end development, but I try to go at this in the simplest way possible. Check out the full table of contents of WordPress for Noobs, and see if there’s any other info you’re looking for. This particular chapter is working with the WordPress loop with a custom post type, custom taxonomies and Advanced Custom Fields.
What you will learn in this article:
- Examples of when you’d want to use custom post types, taxonomies and advanced custom fields together Skip to It Daddy-O
- A couple examples of loops using custom post types, taxonomies and advanced custom fields togetherGet to It, Sugar
- How to get the most out of using all three togetherCome on and Give It to Me
Examples of when you’d want to use custom post types, taxonomies and advanced custom fields together
The reason why I’m sharing examples of how to use these all together, is because I think if you can master using all of this stuff together, you’ll be able to tackle just about anything WordPress can throw at you. So consider this level’s final boss. If you can rock these things all together you are no longer a Noob. You can handle a ton of tasks in WordPress development, and are likely at least a Jr. Level WordPress Developer.
Using Custom Post Types, Taxonomies and Fields gives you freedom to make many kinds of websites
For example – Clinic
Custom Post Type: Doctor (Allows easy administration, adding and displaying a specific type of item)
Taxonomies: Specialties, Locations (Allows you to have categorization beyond stock categories to sort or display information)
Fields: Education, other “About” Information fields (Allows you to work these elements into styled templates without the client having to touch code)
Example – Book Publisher
Custom Post Type: Book (Allows publisher to add books easily in the sidebar of WordPress)
Taxonomies: Sort and Display the books by whatever categorization structure you’d like.
Fields: Any extra information about the book, but that you don’t need to use to sort as categories/taxonomies. The ISBN number, the description, so on and so forth.
Brief refresher on where and how to set up custom post types, taxonomies, and advanced custom fields.
Custom Post Types can be set up through your WordPress theme’s functions.php file, more preferably in a plugin of your own creation so they can be used even if you change themes, or you can also use a plugin like Pods or Custom Post Type UI. Taxonomies can be set up by any of these same means.
Advanced Custom Fields can be set up with a really awesome plugin here.
A Couple Examples of Loops using Custom Post Types, Taxonomies and Advanced Custom Fields together
It’s always best to learn from code snippets, and of course it’s nice to be able to copy and paste. Here is a custom post type, taxonomies, and advanced custom fields working together in harmony from beginning to end (code). For instance this loop below is displaying books using a custom taxonomy “booktype” and only displaying items that have a booktype of “fiction.”
As you can see, it would then display, each book with its title, ISBN number, a big introduction and then the content of the post. If there weren’t any to display, it would give a little note about that. Then the query resets in case there are any other post listings on this page.
<?php $args = array( 'orderby' => 'title', 'post_type' => 'cpt-name-book', 'booktype' => 'fiction', ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class="each-book"> <h1 class="the-title"><?php the_title() ;?></h1> <div class="isbn-number"><?php the_field('isbn_number');?></div> <div class="big-intro"><?php the_field('book-introduction');?></div> <div class="main-summary"><?php the_content() ?></div> </div> <?php endwhile; else: ?> <p>Sorry, there are no posts to display</p> <?php endif; ?> <?php wp_reset_query(); ?>
Of Ccurse if you wanted the above code to work, you would need to create the custom post type and taxonomy how you see fit and then add Advanced Custom Fields in the WordPress admin through the ACF plugin.
When wielded together, custom post types, taxonomies and ACF will allow you to use ‘order’ to “order” them in ascending or descending order.
'order' => 'ASC', or
'order' => 'DESC',
‘Order-by’ allows you to sort by these 10 possible items (from WordPress Codex)
- ‘none‘ – No order (available since Version 2.8)
- ‘ID‘ – Order by post ID. Note the capitalization
- ‘author‘ – Order by author
- ‘title‘ – Order by title
- ‘name‘ – Order by post name (post slug)
- ‘type‘ – Order by post type (available since Version 4.0)
- ‘date‘ – Order by date
- ‘modified‘ – Order by last modified date
- ‘parent‘ – Order by post/page parent ID
- ‘rand‘ – Random order
- ‘comment_count‘ – Order by number of comments (available since Version 2.9)
- ‘menu_order‘ – Order by Page Order. Used most often for Pages (Order field in the Edit Page Attributes box) and for Attachments (the integer fields in the insert / upload media gallery dialog), but could be used for any post type with distinct ‘menu_order‘ values (they all default to 0)
- ‘meta_value‘ – Note that a ‘meta_key=keyname‘ must also be present in the query. Note also that the sorting will be alphabetical, which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use ‘meta_value_num‘ instead for numeric values. You may also specify ‘meta_type‘ if you want to cast the meta value as a specific type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’, same as in ‘$meta_query‘. When using ‘meta_type‘ you can also use meta_value_* accordingly. For example, when using DATETIME as ‘meta_type‘ you can use ‘meta_value_datetime‘ to define order structure
- ‘meta_value_num‘ – Order by numeric meta value (available since Version 2.8). Also note that a ‘meta_key=keyname‘ must also be present in the query. This value allows for numerical sorting as noted above in ‘meta_value‘
- ‘post__in‘ – Preserve post ID order given in the post__in array (available since Version 3.5)
Here’s one more example of a loop with a custom post type, a custom taxonomy, and advanced custom fields for medical:
<?php $args = array( 'orderby' => 'title', 'post_type' => 'doctor', 'specialty' => 'nose-and-throat', ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class="each-dr"> <h1 class="the-title"><?php the_title() ;?></h1> <div class="isbn-number"><?php the_field('education');?></div> <div class="big-intro"><?php the_field('about');?></div> <div class="main-summary"><?php the_content() ?></div> </div> <?php endwhile; else: ?> <p>Sorry, there are no doctors to display.</p> <?php endif; ?> <?php wp_reset_query(); ?>
Utilize the power of all three of these components to create extensible WordPress websites that are as powerful as you need them. Start experimenting.
I hope you’ve been experimenting all along the way, but none of this knowledge will truly sink in until you put it to work. Let me offer encouragement, I’ve found learning how to use all three of these together has made my WordPress skills truly useful to many small businesses I’ve helped, my own portfolio, and now larger clients that I get to help as well.
Your next steps:
There is more than one way to skin a cat, but if you’ve been able to follow along and implement this code, you have the basis for a strong way to create WordPress sites with any number of types of items, categorization and info without making the client touch any code. This is the beginning of greatness. Go forth and help clients!Using the WordPress loop with a Custom Post Type, Taxonomies and Fields Click To Tweet
Thank you so much for reading “Using the WordPress loop with a Custom Post Type, Taxonomies and Fields.” in the WordPress for Noobs series, I am passionate about creating WordPress sites in Minneapolis for clients. Reach out to me if you need anything and subscribe below if you’d like a PDF of the book when it’s finished. Thank you!
Get a Free Instant SEO Audit
Watch this quick video to see how savvy businesses save time and hook better leads: