Post Types

Below is a mix of examples, codex exerpts and my comments.

As you know there are custom post types and default post types. Registering custom post type:

   function my_post_type_init() {

       $singular = __( 'Custom-post-type-singular', 'text-domain' ); 
       $plural   = __( 'Custom-post-type-plural', 'text-domain' );
       $plural_slug = str_replace( ' ', '_', $plural );
       $labels = array(
                  'name'               => $singular,
                  'singular_name'      => $singular,
                  'add_new'            => 'Add New',
                  'add_new_item'       => 'Add New ' . $singular,
                  'edit'               => 'Edit',
                  'edit_item'          => 'Edit ' . $singular,
                  'new_item'           => 'New ' . $singular,
                  'view'               => 'View ' . $singular,
                  'view_item'          => 'View ' . $singular,
                  'search_term'        => 'Search ' . $plural,
                  'parent'             => 'Parent ' . $singular,
                  'not_found'          => 'No ' . $plural . ' found',
                  'not_found_in_trash' => 'No ' . $plural . ' in Trash'
            );

       $args = array(
                  'labels'              => $labels,
                  'public'              => true,
                  'publicly_queryable'  => true,
                  'exclude_from_search' => false,
                  'show_in_nav_menus'   => true,
                  'show_ui'             => true,
                  'show_in_menu'        => true,
                  'show_in_admin_bar'   => true,
                  'menu_position'       => 35,
                  'menu_icon'           => 'dashicons-admin-post',
                  'can_export'          => true,
                  'delete_with_user'    => false,
                  'hierarchical'        => false,
                  'has_archive'         => true,
                  'query_var'           => true,
                  'capability_type'     => 'page',
                  'map_meta_cap'        => true,
                  'rewrite'             => array(
                                     'slug' => strtolower($singular),
                            ),
                  'supports'            => array(
                                  'title',
                                  'thumbnail',
                                  'custom-fields',
                            )
            );
            register_post_type( 'my-post-type', $args );
        }
 add_action('init', 'my_post_type_init');
  • Dashicon name for ‘menu_icon’ can be taken here:  https://developer.wordpress.org/resource/dashicons/#editor-video
  •  ‘my-post-type’  – must not exceed 20 characters
  •  ‘menu_position’ should be selected in respect with default: bottom of dashboard menu structure
    • 2 – Dashboard
    • 4 – Separator
    • 5 – Posts
    • 10 – Media
    • 15 – Links
    • 20 – Pages
    • 25 – Comments
    • 59 – Separator
    • 60 – Appearance
    • 65 – Plugins
    • 70 – Users
    • 75 – Tools
    • 80 – Settings
    • 99 – Separator
  • supports(array/boolean) (optional) – fields and metaboxes on the custom post type page in dashboard. Default: title and editor. Available options:
    • ‘title’
    • ‘editor’ (content)
    • ‘author’
    • ‘thumbnail’ (featured image, current theme must also support post-thumbnails)
    • ‘excerpt’
    • ‘trackbacks’
    • ‘custom-fields’
    • ‘comments’ (also will see comment count balloon on edit screen)
    • ‘revisions’ (will store revisions)
    • ‘page-attributes’ (menu order, hierarchical must be true to show Parent option)
    • ‘post-formats’ add post formats, see Post Formats
  • rewrite(boolean or array) (optional) Triggers the handling of rewrites for this post type. To prevent rewrites, set to false. Default: true and use $post_type as slug
    • 'slug' => string Customize the permalink structure slug. Defaults to the $post_type value. Should be translatable.
    • 'with_front' => bool Should the permalink structure be prepended with the front base. (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/). Defaults to true
    • 'feeds' => bool Should a feed permalink structure be built for this post type. Defaults to has_archive value.
    • 'pages' => bool Should the permalink structure provide for pagination. Defaults to true
    • 'ep_mask' => const As of 3.4 Assign an endpoint mask for this post type. For more info see Rewrite API/add_rewrite_endpoint, Trac Ticket 19275, and this Make WordPress Plugins summary of endpoints.
      • If not specified and permalink_epmask is set, inherits from permalink_epmask
      • If not specified and permalink_epmask is not set, defaults to EP_PERMALINK

Now we want to add custom columns to the list of posts of custom post type. To add a custom column for specific custom post types, hook the manage_{$post_type}_posts_columns filter in conjunction with manage_{$post_type}_posts_custom_column action.

 add_filter('manage_my-post-type_posts_columns', 'my_post_type_custom_columns_callback');
 add_action('manage_my-post-type_posts_custom_column', 'my_post_type_custom_column_data_callback', 10, 2);

 function my_post_type_custom_columns_callback() {
            $newColumns                  = array();
            $newColumns['title']         = 'Title';
            $newColumns['calendar_date'] = 'Date';
            $newColumns['preamble']      = 'Preamble';
            return $newColumns;
  }
 function my_post_type_custom_column_data_callback($column, $post_id) {
       switch ($column) {
            case 'calendar_date':
                 $calendar_date = get_post_meta($post_id,
                                        'calendar_date', true );
                 $date          = date_create($calendar_date);
                 echo date_format($date, "j. F  Y");
                 break;
            case 'preamble':
                $preamble = get_post_meta($post_id, 'preamble', false);
                foreach ($preamble as $ev) {
                    echo $ev . '<br>';
                }
                    break;
            }
        }

In the above example we use custom field ‘preamble’ to add post meta several times thus we create array ‘preamble’. All positions of this array will be displayed in the Preamble column.

Default post types are described below (Excerption is taken from WordPress Codex):


There are five post types that are readily available to users or internally used by the WordPress installation by default :

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation menu (Post Type: ‘nav_menu_item’)

Post

Post in WordPress is a post type that is typical for and most used by blogs. Posts are normally displayed in a blog in reverse sequential order by time (newest posts first).Posts are also used for creating the feeds.

Page

Page in WordPress is like post, but it lives outside the normal time-based listings ofposts. Pages can use different page templates to display them. Pages can also be organized in a hierarchical structure, with pages being parents to other pages, but they normally cannot be assigned categories and tags. If permalinks are enabled, thepermalink of a page is always composed solely of the main site URL and the user-friendly and URL-valid names (also referred to as slug) of the page and its parents if they exist. See the Pages article for more information about the differences.

Attachment

Attachment is a special post that holds information about a file uploaded through the WordPress media upload system, such as its description and name. For images, this is also linked to metadata information, stored in the _F9J_postmeta table, about the size of the images, the thumbnails generated from the images, the location of the image files, the HTML alt text, and even information obtained from EXIF data embedded in the images.

Revision

Revision is used to hold a draft post as well as any past revisions of a published post. Revisions are basically identical to the published post which they belong to, but have that post set as their parent using the post_parent column of the _F9J_posts table.

Navigation Menu

Navigation Menu is a type that holds information about a single item in the WordPress navigation menu system. These are the first examples of entries in the _F9J_posts table to be used for something other than an otherwise displayable content on the blog.
END of the Codex excerption


As you can see pages normally cannot be assigned categories and tags. Here is the example how to add category & tag to page:

function add_taxonomies_to_pages() {
     register_taxonomy_for_object_type( 'post_tag', 'page' );
     register_taxonomy_for_object_type( 'category', 'page' );
}

add_action('init', 'add_taxonomies_to_pages');

To add custom columns to the list page pages, hook the manage_pages_columns filter in conjunction with manage_pages_custom_column action. Pay attantion to differences with custom post types.

function my_custom_pages_columns($columns) {
            $customColumns = array(
                            'header'  => __('Header', 'text-domain'),
                            'address' => __('Address', 'text-domain'),
                            'email'   => __('Email', 'text-domain'),
                            'phone'   => __('Phone', 'text-domain'),
                            'fax'     => __('Fax', 'text-domain'),
            );
            $columns = array_merge($columns, $customColumns);
            /** Remove an Author, Comments Columns * */
            unset( $columns['author'], $columns['comments'] );
            return $columns;
        }

add_filter('manage_pages_columns', 'my_custom_pages_columns');

function custom_page_column_content($column, $post_id) {
       switch ($column) {
          case 'email':
                $email   = get_post_meta($post_id, 'email', true);
                echo '<a href="mailto:' . $email . '" >' . $email . '</a>';
                break;
          case 'address':
                $address = get_post_meta($post_id, 'address', true);
                echo $address;
                break;
          case 'header':
                $header  = get_post_meta($post_id, 'header', true);
                echo $header;
                break;
          case 'phone':
                $phone = get_post_meta($post_id, 'phone', true);
                echo $phone;
                break;
          case 'fax':
                $fax   = get_post_meta($post_id, 'fax', true);
                echo $fax;
                break;
            }
}
add_action('manage_pages_custom_column', 'custom_page_column_content', 10, 2);

Leave a Reply

Your email address will not be published. Required fields are marked *