Site icon WP Smith

Helpful Custom Post Type and Custom Taxonomy Functions

Understanding WordPress Custom Post Types

Here are two great posts regarding custom post types' functions: An Important Update: WordPress 3.0 Post Types And Taxonomies, from New2WP written by  Jared (@Tweeaks), and Custom Post Types in WordPress from Justin Tadlock (@justintadlock). that I wanted to reproduce in part in this series.

The following are functions you can use for various things related to post types. The function name is linked to the line in /wp-includes/post.php where it is created for those that would like to check out the source code which makes them work. I've also added a short description for each that is used in the source to describe them [slightly modified by Travis Smith].

Post Type Functions:

  • is_post_type() - Checks to see if current post is of a specified post type.
    [php]<?php
    if ( is_post_type( 'zombie', $post_id ) )
    echo 'This is a not a blog post. It is a zombie!';
    else
    echo 'This is not a zombie. [insert sad face]';[/php]
  • get_post_types( $post ) - Get a list of all registered post type objects.
    • $args - An array of key => value arguments to match against the post type objects.
    • $output - The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
    • $operator - The logical operation to perform. 'or' means only one element from the array needs to match; 'and' means all elements must match. The default is 'and'.
    • Returns an array listing post type names or objects.
  • get_post_type_object() - Retrieves a post type object by name
    [php]$post_type = get_post_type_object( 'zombie' );

    if ( $post_type->show_ui )
    custom_function_name();
    [/php]

  • register_post_type() - Register a post type. Do not use before init.
    • A simple function for creating or modifying a post type based on the parameters given.
    • The function will accept an array (second optional parameter), along with a string for the post type name.
  • get_post_type_capabilities() - Builds an object with all post type capabilities out of a post type object.
  • Accepted keys of the capabilities array in the post type object:
    • edit_post - The meta capability that controls editing a particular object of this post type. Defaults to "edit_ . $capability_type" (edit_post).
    • edit_posts - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
    • edit_others_posts - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
    • publish_posts - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
    • read_post - The meta capability that controls reading a particular object of this post type. Defaults to "read_ . $capability_type" (read_post).
    • read_private_posts - The capability that controls reading private posts.Defaults to "read_private . $capability_type . s" (read_private_posts).
    • delete_post - The meta capability that controls deleting a particular object of this post type. Defaults to "delete_ . $capability_type" (delete_post).
  • get_post_type_labels( $post_type_object ) - Builds an object with all post type labels out of a post type object.
  • Accepted keys of the label array in the post type object:
    • name - general name for the post type, usually plural. The same and overriden by $post_type_object->label. Default is Posts/Pages
    • singular_name - name for one object of this post type. Default is Post/Page
    • add_new - Default is Add New for both hierarchical and non-hierarchical types.
    • add_new_item - Default is Add New Post/Add New Page
    • edit_item - Default is Edit Post/Edit Page
    • new_item - Default is New Post/New Page
    • view_item - Default is View Post/View Page
    • search_items - Default is Search Posts/Search Pages
    • not_found - Default is No posts found/No pages found
    • not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
    • parent_item_colon - This string isn't used on non-hierarchical types. (In hierarchical ones the default is Parent Page)
  • _get_custom_object_labels() - Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
  • add_post_type_support( $post_type, $feature ) - Register support of certain features for a post type.
  • All features are directly associated with a functional area of the edit screen, such as the editor or a meta box:
    • 'title',
    • 'editor',
    • 'comments',
    • 'revisions',
    • 'trackbacks'
    • 'author',
    • 'excerpt',
    • 'page-attributes',
    • 'thumbnail',
    • 'custom-fields'

    Additionally, the 'revisions' feature dictates whether the post type will store revisions, and the 'comments' feature dicates whether the comments count will show on the edit screen.

  • set_post_type( $post_id = 0, $post_type = 'post' ) - Updates the post type for the post ID.
    • $post_id Post ID to change post type. Not actually optional.
    • $post_type Optional, default is post. Supported values are 'post' or 'page' to name a few.
    • Return int Amount of rows changed. Should be 1 for success and 0 for failure.
  • get_posts() - Retrieve list of latest posts or posts matching criteria.
  • The defaults are:
    • 'numberposts' - Default is 5. Total number of posts to retrieve.
    • 'offset' - Default is 0. See { WP_Query::query() } for more.
    • 'category' - What category to pull the posts from.
    • 'orderby' - Default is 'post_date'. How to order the posts.
    • 'order' - Default is 'DESC'. The order to retrieve the posts.
    • 'include' - See { WP_Query::query() } for more.
    • 'exclude' - See { WP_Query::query() } for more.
    • 'meta_key' - See { WP_Query::query() } for more.
    • 'meta_value' - See { WP_Query::query() } for more.
    • 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
    • 'post_parent' - The parent of the post or post type.
    • 'post_status' - Default is 'published'. Post status to retrieve.

Functions For Taxonomies You Should Know About

The following are functions which you can use when working with taxonomies. I'm not going to link to the line of each function or include descriptions of each since the file which contains them is way smaller than posts.php where the post type functions are, so if you're interested in viewing the source of these you should've have a hard time finding the line they are on.

The functions which make up taxonomies are located in /wp-includes/taxonomy.php of the WordPress source.