WordPress Custom Post Type Plugins

This tutorial will help you to create custom post type using register_post_type. register_post_type should only be invoked through the ‘init’ action.

<?php
/**
 * Plugin Name: Custom Post
 * Plugin URI: 
 * Description: Add the custom post type like user
 * Version: The Plugin's Version Number, e.g.: 1.0
 * Author: Name Of The Plugin Author
 * Author URI: http://URI_Of_The_Plugin_Author
 * License: A "Slug" license name e.g. GPL2
 */
add_action('init','register_post_taxonomies');
function register_post_taxonomies(){
// Add the array of post type slug and label
$array=array(
	'user'=>'User',
	'company'=>'Company'
);
foreach($array as $slug=>$value){
$labels = array(
		'name'               => _x( $value, 'post type general name', 'your-plugin-textdomain' ),
		'singular_name'      => _x( $value, 'post type singular name', 'your-plugin-textdomain' ),
		'menu_name'          => _x( $value, 'admin menu', 'your-plugin-textdomain' ),
		'name_admin_bar'     => _x( $value, 'add new on admin bar', 'your-plugin-textdomain' ),
		'add_new'            => _x( 'Add New', $value, 'your-plugin-textdomain' ),
		'add_new_item'       => __( 'Add New '.$value, 'your-plugin-textdomain' ),
		'new_item'           => __( 'New '.$value, 'your-plugin-textdomain' ),
		'edit_item'          => __( 'Edit '.$value, 'your-plugin-textdomain' ),
		'view_item'          => __( 'View '.$value, 'your-plugin-textdomain' ),
		'all_items'          => __( 'All '.$value, 'your-plugin-textdomain' ),
		'search_items'       => __( 'Search '.$value, 'your-plugin-textdomain' ),
		'parent_item_colon'  => __( 'Parent '.$value.':', 'your-plugin-textdomain' ),
		'not_found'          => __( 'No '.$value.' found.', 'your-plugin-textdomain' ),
		'not_found_in_trash' => __( 'No '.$value.' found in Trash.', 'your-plugin-textdomain' ),
	);

	$args = array(
		'labels'             => $labels,
		'public'             => true,
		'publicly_queryable' => true,
		//'show_ui'            => true,
		//'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => $slug ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		//'menu_position'      => null,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ,'custom-fields', 'trackbacks', 'revisions'),
		'taxonomies' => array('category','post_tag')
	);
	register_post_type( $slug, $args );
}	
}

Custom Post Type Order In Admin Dashboard

function index_page_query_limit($query){
	if ( is_admin() && !isset( $_GET['orderby'] ) ) 
	{         
		$post_type = $query->query['post_type'];
		$query->set('orderby', 'date');
		$query->set('order', 'DESC');
	}
  	remove_action('pre_get_posts', 'index_page_query_limit');
	return $query;
}
add_action('pre_get_posts', 'index_page_query_limit');

 

Leave a Reply

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