array(
'name' => __('name'),
'singular_name' => __('singular_name'),
'add_new' => __('add_new'),
'add_new_item' => __('add_new_item'),
'edit_item' => __('edit_item'),
'new_item' => __('new_item'),
'view_item' => __('view_item'),
'search_items' => __('search_items'),
'not_found' => __('not_found'),
'not_found_in_trash' => __('not_found_in_trash'),
'parent_item_colon' => __('parent_item_colon'),
),
'menu_position' => 0,
'description' => __('Some description about the content type'),
'public' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'custom', 'with_front' => false),
'register_meta_box_cb' => array($this, 'addMeta'),
'taxonomies' => array('post_tag', 'category', 'type'),
'query_var' => 'custompost',
'show_in_nav_menus' => false,
'supports' => array(
'title',
'editor',
'comments',
'revisions',
'trackbacks',
'author',
'excerpt',
'page-attributes',
'thumbnail',
'custom-fields',
),
)
);
// Add action to save the custompost metadata
add_action('save_post', array($this, 'saveMetaContent'));
if (!is_admin()) {
// If not in admin then register function to alter query to print posts.
add_filter('pre_get_posts', array($this, 'getCustomPosts'));
}
}
/**
* Registered function that alters the WP_Query object to include the
* custompost type on certain pages.
* There are certain checks in place in order to prevent destroying
* the existing functionality.
*
* @param WP_Query $query The WP_Query object.
*
* @return WP_Query The modified (or not) WP_Query object.
*/
public function getCustomPosts($query) {
if (!is_page() || is_home() || is_archive()) {
$query->set('post_type', array('post', 'custompost'));
}
return $query;
}
/**
* Registered function that adds a metadata box to the post edit screen for
* the custompost type.
*/
public function addMeta() {
add_meta_box('myplugin_sectionid',
__('My Post Section Title', 'myplugin_textdomain'),
array($this, 'addMetaContent'),
'custompost',
'advanced'
);
}
/**
* Registered function that provides the content for the metadata box.
*/
public function addMetaContent() {
$args = func_get_args();
$currentObject = $args[0];
echo '';
//$metaContent = get_metadata('custompost', $currentObject->ID, 'custompost_subtitle', true);
//$metaContent = get_post_meta($currentObject->ID, 'custompost_subtitle', true);
$metaContent = get_metadata('post', $currentObject->ID, 'custompost_subtitle', true);
// The actual fields for data entry
echo ' ';
echo '';
}
/**
* Registered function that saves the metadata contet to the atabase,
*
* @param int $post_id The current post ID.
*/
public function saveMetaContent($post_id) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (!wp_verify_nonce($_POST['myplugin_noncename'], plugin_basename(__FILE__))) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// Check permissions
if ('custompost' == $_POST['post_type']) {
if (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
}
// OK, we're authenticated: we need to find and save the data
// $meta_value = get_metadata('custompost', $post_id, 'custompost_subtitle', true);
// $meta_value = get_post_meta($post_id, 'custompost_subtitle', true);
$meta_value = get_metadata('post', $post_id, 'custompost_subtitle', true);
$new_meta_value = stripslashes($_POST['custompost_subtitle']);
if (!is_null($new_meta_value) && '' == $meta_value) {
//add_metadata('custompost', $post_id, 'custompost_subtitle', $new_meta_value, true);
//add_post_meta($post_id, 'custompost_subtitle', $new_meta_value, true);
add_metadata('post', $post_id, 'custompost_subtitle', $new_meta_value, true);
} elseif ('' == $new_meta_value) {
//delete_metadata('custompost', $post_id, 'custompost_subtitle', $meta_value);
//delete_post_meta($post_id, 'custompost_subtitle', $meta_value);
delete_metadata('post', $post_id, 'custompost_subtitle', $meta_value);
} elseif ($new_meta_value != $meta_value) {
//update_metadata('custompost', $post_id, 'custompost_subtitle', $new_meta_value);
//update_post_meta($post_id, 'custompost_subtitle', $new_meta_value);
update_metadata('post', $post_id, 'custompost_subtitle', $new_meta_value);
}
}
}
// Register the function that will create the custompost content type.
add_action('init', 'create_custompost');
/**
* Registered function that creates the custompost content type.
*/
function create_custompost() {
$customPost = new CustomPost();
}
// Register custom taxonomy function
add_action('init', 'createCustomTaxonomies', 0);
/**
* Registered cuntion that creates a custom taxonomy.
*/
function createCustomTaxonomies() {
register_taxonomy('type', 'custompost',
array(
'hierarchical' => false,
'label' => __('Type'),
'query_var' => true,
'rewrite' => true
)
);
}
// Register function to customise table headings
add_filter("manage_edit-custompost_columns", "custompostColumns");
// Register function to customise table data
add_action("manage_posts_custom_column", "custompostRowValues");
/**
* Registered function that changes the column headings in the post listing
* screen for this content type.
*
* @param array $columns The existing columns from the list screen.
*
* @return array The altered columns array.
*/
function custompostColumns($columns) {
$columns = array(
"cb" => "",
"title" => "Podcast Title",
"description" => "Description",
"metavalue" => "MetaValue",
);
return $columns;
}
/**
* Registered function that prints out the values in rows.
*
* @global object $post The currently loaded Post object.
*
* @param string $column The current column being looked at.
*/
function custompostRowValues($column) {
global $post;
switch ($column) {
case 'ID':
// Print post title.
print $post->ID;
break;
case 'description':
// Print the post content
print $post->post_content;
break;
case 'metavalue':
// Extract the metadata field and print value here.
echo get_metadata('post', $post->ID, 'custompost_subtitle', true);
break;
}
}