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 How to Make a Website with WordPress Custom Post Types and Fields in WordPress Setting Up Custom Post Types and Fields

Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

Custom post type UI post type location?

When using custom post type UI where is the actual post type located int he file directory? I looked in the plugin itself but could not find the post type that I created.

Rhoda Toynbee
Rhoda Toynbee
18,938 Points

Do you mean the post type that you specified? Or the posts that will be associated with it when you assign them?

Usually, you would use something like Advanced Custom Fields with the post type that you created. There is a drop down menu that lets you choose your custom post type to be associated with pages or posts.

If you want to edit or delete the post type that you created then you just click on Custom UI on your Admin Menu and edit/delete your post type.

Hope that helps a little...

Rhoda

Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

No I mean the actual file that it creates in your WordPress directory. I want to see the code.

Rhoda Toynbee
Rhoda Toynbee
18,938 Points

Good question. I have no clue. I just searched every file in one of my wordpress projects for a custom post type slug that I have and it only pulled up the places that I referenced it in a page template.

Good luck :)

4 Answers

Kevin Korte
Kevin Korte
28,149 Points

Here is code from the plugin, I'm way oversimplifying, but

This registers the custom post type

<?php
function cptui_register_single_post_type( $post_type = array() ) {

    /**
     * Filters the map_meta_cap value.
     *
     * @since 1.0.0
     *
     * @param bool   $value     True.
     * @param string $name      Post type name being registered.
     * @param array  $post_type All parameters for post type registration.
     */
    $post_type['map_meta_cap'] = apply_filters( 'cptui_map_meta_cap', true, $post_type['name'], $post_type );

    /**
     * Filters custom supports parameters for 3rd party plugins.
     *
     * @since 1.0.0
     *
     * @param array  $value     Empty array to add supports keys to.
     * @param string $name      Post type slug being registered.
     * @param array  $post_type Array of post type arguments to be registered.
     */
    $user_supports_params = apply_filters( 'cptui_user_supports_params', array(), $post_type['name'], $post_type );

    if ( is_array( $user_supports_params ) ) {
        $post_type['supports'] = array_merge( $post_type['supports'], $user_supports_params );
    }

    if ( ! empty( $post_type['custom_supports'] ) ) {
        $custom = explode( ',', $post_type['custom_supports'] );
        foreach( $custom as $part ) {
            $post_type['supports'][] = $part;
        }
    }

    if ( in_array( 'none', $post_type['supports'] ) ) {
        $post_type['supports'] = false;
    }

    $labels = array(
        'name'               => $post_type['label'],
        'singular_name'      => $post_type['singular_label']
    );

    $preserved = cptui_get_preserved_keys( 'post_types' );
    foreach( $post_type['labels'] as $key => $label ) {

        if ( !empty( $label ) ) {
            $labels[ $key ] = $label;
        } elseif ( empty( $label ) && in_array( $key, $preserved ) ) {
            $labels[ $key ] = cptui_get_preserved_label( 'post_types', $key, $post_type['label'], $post_type['singular_label'] );
        }
    }

    $has_archive = get_disp_boolean( $post_type['has_archive'] );
    if ( !empty( $post_type['has_archive_string'] ) ) {
        $has_archive = $post_type['has_archive_string'];
    }

    $show_in_menu = get_disp_boolean( $post_type['show_in_menu'] );
    if ( !empty( $post_type['show_in_menu_string'] ) ) {
        $show_in_menu = $post_type['show_in_menu_string'];
    }

    $rewrite = get_disp_boolean( $post_type['rewrite' ] );
    if ( false !== $rewrite ) {
        //Core converts to an empty array anyway, so safe to leave this instead of passing in boolean true.
        $rewrite = array();
        $rewrite['slug'] = ( !empty( $post_type['rewrite_slug'] ) ) ? $post_type['rewrite_slug'] : $post_type['name'];
        $rewrite['with_front'] = ( 'false' === disp_boolean( $post_type['rewrite_withfront'] ) ) ? false : true;
    }

    $menu_icon = ( !empty( $post_type['menu_icon'] ) ) ? $post_type['menu_icon'] : null;

    if ( in_array( $post_type['query_var'], array( 'true', 'false', '0', '1' ) ) ) {
        $post_type['query_var'] = get_disp_boolean( $post_type['query_var'] );
    }

    $menu_position = null;
    if ( !empty( $post_type['menu_position'] ) ) {
        $menu_position = (int) $post_type['menu_position'];
    }

    if ( ! empty( $post_type['exclude_from_search'] ) ) {
        $exclude_from_search = get_disp_boolean( $post_type['exclude_from_search'] );
    } else {
        $public = get_disp_boolean( $post_type['public'] );
        $exclude_from_search = ( false === $public ) ? true : false;
    }

    $args = array(
        'labels'              => $labels,
        'description'         => $post_type['description'],
        'public'              => get_disp_boolean( $post_type['public'] ),
        'show_ui'             => get_disp_boolean( $post_type['show_ui'] ),
        'has_archive'         => $has_archive,
        'show_in_menu'        => $show_in_menu,
        'exclude_from_search' => $exclude_from_search,
        'capability_type'     => $post_type['capability_type'],
        'map_meta_cap'        => $post_type['map_meta_cap'],
        'hierarchical'        => get_disp_boolean( $post_type['hierarchical'] ),
        'rewrite'             => $rewrite,
        'menu_position'       => $menu_position,
        'menu_icon'           => $menu_icon,
        'query_var'           => $post_type['query_var'],
        'supports'            => $post_type['supports'],
        'taxonomies'          => $post_type['taxonomies']
    );

    /**
     * Filters the arguments used for a post type right before registering.
     *
     * @since 1.0.0
     *
     * @param array  $args Array of arguments to use for registering post type.
     * @param string $value Post type slug to be registered.
     */
    $args = apply_filters( 'cptui_pre_register_post_type', $args, $post_type['name'] );

    return register_post_type( $post_type['name'], $args );
}

and this creates the post type

<?php
function cptui_create_custom_post_types() {
    $cpts = get_option( 'cptui_post_types' );

    if ( is_array( $cpts ) ) {
        foreach ( $cpts as $post_type ) {
            cptui_register_single_post_type( $post_type );
        }
    }
    return;
}
add_action( 'init', 'cptui_create_custom_post_types', 10 );

And I can't check this right now, but I believe it stores a users CPT in the wp_options table is my hunch.

Kevin Korte
Kevin Korte
28,149 Points

The post type UI files are in the plugin folder. The plugin itself doesn't actually create a file. It simple creates the post type manually, much like you could for yourself in the functions.php file. The plugin is just using wordpress hooks to add in whatever the custom post type code needs to be.

Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

OK, so it is taking your input and then hooking it into the functions.php? Where does it store your information and write hooks?

Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

@Kevin Korte Wait I think I get it. It uses its own files to evaluate your input. But it must store your input somewhere right?

Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

Thanks, way over my head but at least I have an idea of what is going on now. I want to try to create the post type on my own then the template page.

Kevin Korte
Kevin Korte
28,149 Points

No worries, honestly a lot of it is over my head too. I can almost always grasp the big pieces, but not always the rest.