Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Liam Maclachlan
Liam Maclachlan
22,805 Points

How do you create and store a custom post type in a custom plugin?

I am working on a plugin (I understand there are ones out there that do this but I'm needing to create a bespoke plugin) for a project I am working on.

I need it to:

1 - create a Custom Post Type, in the plugin 2- store the CPT 3- recall the CPT and it's content within a custom WP loop

Anyone know a starting point for this? Just need a nudge in the right direction, or actual coding advice would be great :)

Liam Maclachlan
Liam Maclachlan
22,805 Points

I found this article, which I think may be a great answer/ starting point but any other info would be great :)

1 Answer

Am I getting that right: you want to register a CPT via your plugin?

You could set up your custom-plugin.php like this:

<?php 
/* 
Plugin Name: Your custom plugin
Description: This plugin adds a custom post type to your WordPress. The CPT is called 'worms'
Version: 1.0
Author: Saskia Lund
Author URI: https://saskialund.de
License: GPLv2
*/

function create_post_type_worms() {
    $labels = array(
        'name' => 'Worms',
        'singular_name' => 'worm',
        'menu_name' => 'Worms',
        'all_items' => 'All worms',
        'add_new' => 'Add',
        'add_new_item' => 'Add new worm',
        'edit' => 'Edit',
        'edit_item' => 'Edit worm',
        'new_item' => 'New worm',
        'view' => 'Show',
        'view_item' => 'Show worm',
        'search_items' => 'Search worm',
        'not_found' => 'No worms found',
        'not_found_in_trash' => 'No worms in the trash',
        'parent_item_colon' => ''
        );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'has_archive' => true,
        'show_in_menu' => true,
        'exclude_from_search' => false,
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => 'worms' ),
        'capability_type' => 'post',
        'query_var' => true,
        'menu_position' => 5,       
        'menu_icon' => 'dashicons-editor-customchar',       
        'supports' => array( 'title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail' )
        );
    register_post_type( 'worms', $args );
}

add_action( 'init', 'create_post_type_worms' );

function my_rewrite_flush() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );

?>

From here the rest is also easy..

Just duplicate the archive.php and rename it like so: archive-worms.php and then add your custom loop code to it.

same for the single.php single-worm.php

add your custom code.

Hope this helped ;)

Liam Maclachlan
Liam Maclachlan
22,805 Points

Hi Saskia. In hind site of this question, this is exactly what I wanted to do. I was thinking of registering separate post types for each article. ... then I realised I only need, as you said, register a single post type in the plugin. I Think I may have been having a moment :p

Now I just need to look in to custom fields for each post type (some option have nested categories that are duplicated on 'add product') and how to access then. Any insight? :)

Hi Liam,

there are two options to approach custom fields:

  1. Read upon custom fields in the WP Codex here: https://codex.wordpress.org/Custom_Fields and make use of the information given. You can implement as many custom fields as you like and attach them to your custom post type respectively for example. Then you could create fields that ask for the custom fields info in the backend. Let's say: MY first Worm post: length of worm: 15 inches Here the length of worm would be a custom field and in the worm-post creation backend you'd have a field where you coud enter the length of that particular worm. To have the input of that field diesplayed in your frontend, you need to modify the custom single-worms.php and/or the custom archive-worms.php depending on where you want that info to be displayed. You could as well display it in your home page or in the sidebar. Just need some javascript, which is explained on the mentioned codex site,

  2. The easier approach is, to use the Advanced Custom Fields plugin which is available for free via the WP repository. Have a look at it. IT can add custom fields to your CPT and set up the backend fields very easily.

Hope I could help

Liam Maclachlan
Liam Maclachlan
22,805 Points

Perfect. Thanks for that, Saskia. That is exactly what I was looking for. I'll keep those pages bookmarked for when I get to that point in the project. Thanks again. Great help :)