Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
}

enter image description here

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();
}

enter image description here

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?

share|improve this question
    
why are some WPSE members encouraging the use of classes like this? Thiswill just make it even harder for PHP beginners to understand OOP. –  onetrickpony Feb 6 '11 at 22:37
    
@One Trick Pony So this can be accomplished without using classes? (by the way, what's a WPSE member)? –  janoChen Feb 6 '11 at 23:00
    
WPSE = wordpress stackexchage –  Bainternet Feb 6 '11 at 23:10
2  
@WPSE I thought WPSE = WordPress Super Elite members. –  janoChen Feb 6 '11 at 23:13
3  
@One Trick Pony: Your question was already answered quite eloquently. –  scribu Feb 6 '11 at 23:25
show 2 more comments

1 Answer

up vote 2 down vote accepted

I use the following in my filter hook manage_edit-<post-type>_columns

add_filter( 'manage_edit-visitor_columns', array( &$this, '_wp_filter_visitor_columns' ) );

public function _wp_filter_visitor_columns( $columns )
{
    $columns = array(
            'cb' => '<input type="checkbox" />',
            'title' => 'Name',
            'type' => 'Type',
            //'state' => 'Current State',
            'id' => 'Visitor ID',
            'acct' => 'Using Account',
        );

    return $columns;
}

And to filter the column view, I use the following:

add_action( 'manage_posts_custom_column', array( &$this, '_wp_filter_visitor_column_view' ) );

public function _wp_filter_visitor_column_view( $column )
{
    global $post;
    if ( $column == "type" )
    {
        //$terms = get_the_term_list( $post->ID, 'v_types', '', ', ', '' );
        //echo strip_tags( $terms );

        $terms = wp_get_object_terms( $post->ID, 'v_types' );
        echo $terms[0]->name;
    }
    elseif ( $column == "state" OR $column == "workstation" )
    {
        $workstation = get_post_meta( $post->ID, 'v_workstation', true );
        if ( $column == "workstation" ) echo $workstation;
        elseif ( $column == "state" ) echo 'Not coded....';
    }
    elseif ( $column == "id" )
    {
        echo get_post_meta( $post->ID, 'v_id', true );
    }
    elseif ( $column == "acct" )
    {
        $terms = wp_get_object_terms( $post->ID, 'v_accounts' );
        echo $terms[0]->name;
    }
}

I hope this helps you.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.