The following code provided by Mike Schinkel, creates a custom post type and a custom taxonomy. It makes the taxonomy to show up in the custom post type's column. Finally, it inserts some default terms.
I would like to insert a second taxonomy for the custom post type (Page Content) called: Locations.
(this second custom taxonomy should also show up in the column of the custom post type)
How can I modify the code to archive that?
<?php
/**
* Create the Page Content custom post type and the Page Section custom taxonomy
*/
add_action('init', 'my_init_method');
if (!class_exists('YourSite_PageContent')) {
class YourSite_PageContent {
static function on_load() {
add_action('init',array(__CLASS__,'init'));
add_filter('manage_page_content_posts_columns',
array(__CLASS__,'manage_page_content_posts_columns'));
add_filter('manage_posts_custom_column',
array(__CLASS__,'manage_posts_custom_column'),10,2);
add_action('restrict_manage_posts',
array(__CLASS__,'restrict_manage_posts'));
add_filter('parse_query',
array(__CLASS__,'parse_query'));
}
static function init() {
register_post_type('page_content',array(
'labels' => array(
'name' => __( 'Page Content' ),
'singular_name' => __( 'Page Content' ),
'add_new_item' => 'Add New Page Content',
'edit_item' => 'Edit Page Content',
'new_item' => 'New Page Content',
'search_items' => 'Search Page Content',
'not_found' => 'No Page Content found',
'not_found_in_trash' => 'No Page Content found in trash',
),
'public' => true,
'hierarchical' => false,
'taxonomies' => array( 'page_sections'),
'supports' => array('title','editor','thumbnail','custom-fields'),
'rewrite' => array('slug'=>'page_content','with_front'=>false),
));
register_taxonomy('page_sections','page_content',array(
'hierarchical' => true,
'labels' => array(
'name' => __( 'Page Sections' ),
'singular_name' => __( 'Page Section' ),
'add_new_item' => 'Add New Page Section',
'edit_item' => 'Edit Page Section',
'new_item' => 'New Page Section',
'search_items' => 'Search Page Section',
'not_found' => 'No Page Sections found',
'not_found_in_trash' => 'No Page Sections found in trash',
'all_items' => __( 'All Page Sections' ),
),
'query_var' => true,
'rewrite' => array( 'slug' => 'page_sections' ),
));
if (!get_option('yoursite-page-content-initialized')) {
$terms = array(
'Footer',
'Header',
'Front Page Intro',
'Front Page Content',
);
foreach($terms as $term) {
if (!get_term_by('name',$term,'page_sections')) {
wp_insert_term($term, 'page_sections');
}
}
update_option('yoursite-page-content-initialized',true);
}
}
function manage_page_content_posts_columns($columns){
$new = array();
foreach($columns as $key => $title) {
if ($key=='author') // Put the Page Sections column before the Author column
$new['page_sections_column'] = 'Page Sections';
$new[$key] = $title;
}
return $new;
}
function manage_posts_custom_column( $column,$post_id ) {
global $typenow;
if ($typenow=='page_content') {
$taxonomy = 'page_sections';
switch ($column) {
case 'page_sections_column':
$page_sections_column = get_the_terms($post_id,$taxonomy);
if (is_array($page_sections_column)) {
foreach($page_sections_column as $key => $page_sections) {
$edit_link = get_term_link($page_sections,$taxonomy);
$page_sections_column[$key] = '<a href="'.$edit_link.'">' . $page_sections->name . '</a>';
}
echo implode(' | ',$page_sections_column);
}
break;
}
}
}
function parse_query($query) {
global $pagenow;
$qv = &$query->query_vars;
if ($pagenow=='edit.php' &&
isset($qv['taxonomy']) && $qv['taxonomy']=='page_sections' &&
isset($qv['term']) && is_numeric($qv['term'])) {
$term = get_term_by('id',$qv['term'],'page_sections');
$qv['term'] = $term->slug;
}
}
function restrict_manage_posts() {
global $typenow;
global $wp_query;
if ($typenow=='page_content') {
$taxonomy = 'page_sections';
$page_sections = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Show All {$page_sections->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $wp_query->query['term'],
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // This will give a view
'hide_empty' => true, // This will give false positives, i.e. one's not empty related to the other terms. TODO: Fix that
));
}
}
}
YourSite_PageContent::on_load();
}
EDIT (What I've done so far with no PHP skills):
By modyfing the code this is what I've done so far:
<?php
/**
* Create the Page Content custom post type and the Page Section custom taxonomy
*/
add_action('init', 'my_init_method');
if (!class_exists('YourSite_PageContent')) {
class YourSite_PageContent {
static function on_load() {
add_action('init',array(__CLASS__,'init'));
add_filter('manage_page_content_posts_columns',
array(__CLASS__,'manage_page_content_posts_columns'));
add_filter('manage_posts_custom_column',
array(__CLASS__,'manage_posts_custom_column'),10,2);
add_action('restrict_manage_posts',
array(__CLASS__,'restrict_manage_posts'));
add_filter('parse_query',
array(__CLASS__,'parse_query'));
}
static function init() {
register_post_type('page_content',array(
'labels' => array(
'name' => __( 'Page Content' ),
'singular_name' => __( 'Page Content' ),
'add_new_item' => 'Add New Page Content',
'edit_item' => 'Edit Page Content',
'new_item' => 'New Page Content',
'search_items' => 'Search Page Content',
'not_found' => 'No Page Content found',
'not_found_in_trash' => 'No Page Content found in trash',
),
'public' => true,
'hierarchical' => false,
'taxonomies' => array( 'page_sections'),
'supports' => array('title','editor','thumbnail','custom-fields'),
'rewrite' => array('slug'=>'page_content','with_front'=>false),
));
register_taxonomy('locations','page_content',array(
'hierarchical' => true,
'labels' => array(
'name' => __( 'Locations' ),
'singular_name' => __( 'Page Section' ),
'add_new_item' => 'Add New Page Section',
'edit_item' => 'Edit Page Section',
'new_item' => 'New Page Section',
'search_items' => 'Search Page Section',
'not_found' => 'No Page Sections found',
'not_found_in_trash' => 'No Page Sections found in trash',
'all_items' => __( 'All Page Sections' ),
),
'query_var' => true,
'rewrite' => array( 'slug' => 'locations' ),
));
register_taxonomy('page_sections','page_content',array(
'hierarchical' => true,
'labels' => array(
'name' => __( 'Page Sections' ),
'singular_name' => __( 'Page Section' ),
'add_new_item' => 'Add New Page Section',
'edit_item' => 'Edit Page Section',
'new_item' => 'New Page Section',
'search_items' => 'Search Page Section',
'not_found' => 'No Page Sections found',
'not_found_in_trash' => 'No Page Sections found in trash',
'all_items' => __( 'All Page Sections' ),
),
'query_var' => true,
'rewrite' => array( 'slug' => 'page_sections' ),
));
if (!get_option('yoursite-page-content-initialized')) {
$terms = array(
'Footer',
'Header',
'Front Page Intro',
'Front Page Content',
);
foreach($terms as $term) {
if (!get_term_by('name',$term,'page_sections')) {
wp_insert_term($term, 'page_sections');
}
}
update_option('yoursite-page-content-initialized',true);
}
}
function manage_page_content_posts_columns($columns){
$new = array();
foreach($columns as $key => $title) {
if ($key=='author') // Put the Page Sections column before the Author column
$new['page_sections_column'] = 'Page Sections';
$new[$key] = $title;
}
return $new;
}
function manage_posts_custom_column( $column,$post_id ) {
global $typenow;
if ($typenow=='page_content') {
$locations_taxonomy = 'locations';
$page_sections_taxonomy = 'page_sections';
switch ($column) {
case 'locations_column':
$locations_column = get_the_terms($post_id,$locations_taxonomy);
if (is_array($locations_column)) {
foreach($locations_column as $key => $locations) {
$edit_link = get_term_link($locations,$locations_taxonomy);
$locations_column[$key] = '<a href="'.$edit_link.'">' . $locations->name . '</a>';
}
echo implode(' | ',$locations_column);
}
break;
case 'page_sections_column':
$page_sections_column = get_the_terms($post_id,$page_sections_taxonomy);
if (is_array($page_sections_column)) {
foreach($page_sections_column as $key => $page_sections) {
$edit_link = get_term_link($page_sections,$page_sections_taxonomy);
$page_sections_column[$key] = '<a href="'.$edit_link.'">' . $page_sections->name . '</a>';
}
echo implode(' | ',$page_sections_column);
}
break;
}
}
}
function parse_query($query) {
global $pagenow;
$qv = &$query->query_vars;
if ($pagenow=='edit.php' &&
isset($qv['taxonomy']) && $qv['taxonomy']=='page_sections' &&
isset($qv['term']) && is_numeric($qv['term'])) {
$term = get_term_by('id',$qv['term'],'page_sections');
$qv['term'] = $term->slug;
}
}
function restrict_manage_posts() {
global $typenow;
global $wp_query;
if ($typenow=='page_content') {
$taxonomy = 'page_sections';
$page_sections = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Show All {$page_sections->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $wp_query->query['term'],
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // This will give a view
'hide_empty' => true, // This will give false positives, i.e. one's not empty related to the other terms. TODO: Fix that
));
}
}
}
YourSite_PageContent::on_load();
}
I'm still struggling with this line:
function manage_page_content_posts_columns($columns){
$new = array();
foreach($columns as $key => $title) {
if ($key=='author') // Put the Page Sections column before the Author column
$new['locations_column'] = 'Locations';
$new['cb'] = '<input type="checkbox">';
$new['title'] = 'Title';
$new['page_sections_column'] = 'Page Sections';
$new[$key] = $title;
}
return $new;
}
I can't manage to place Page Sections before Locations.
How to solve this?