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.

Few days ago I asked a question about How to show a custom taxonomy in a custom post type column (and insert some 'default' terms to the custom taxonomy).

Then I saw an answer for Adding a Taxonomy Filter to Admin List for a Custom Post Type.

(Both answered by MikeSchinkel)

I would like to merge both features: to create a custom post type, a custom taxonomy, adding some terms in the custom taxonomy, displaying the custom taxonomy in the custom post type's column, and adding a taxonomy filter.

I know it sounds long, they are all done in those two questions, just need to merge them.

How that code would look like?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Hi @janoChen:

So, you want this?

WordPress Admin for Custom Post Types with Custom Filters and Custom Columns

Alright, here ya go:

<?php 
/*
Plugin name: Static Content
*/
if (!class_exists('YourSite_StaticContent')) {
 class YourSite_StaticContent {
   static function on_load() {
     add_action('init',array(__CLASS__,'init'));
     add_filter('manage_static_content_posts_columns',
         array(__CLASS__,'manage_static_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('static_content',array(
       'labels' => array(
         'name' => __( 'Static Content' ),
         'singular_name' => __( 'Static Content' ),
         'add_new_item' => 'Add New Static Content',
         'edit_item' => 'Edit Static Content',
         'new_item' => 'New Static Content',
         'search_items' => 'Search Static Content',
         'not_found' => 'No Static Content found',
         'not_found_in_trash' => 'No Static Content found in trash',
       ),
       'public' => true,
       'hierarchical' => false,
       'taxonomies' => array( 'section'),
       'supports' => array('title','editor','excerpt'),
       'rewrite' => array('slug'=>'static_content','with_front'=>false),
     ));
     register_taxonomy('section','static_content',array(
       'hierarchical' => true,
       'labels' => array(
         'name' => __( 'Section' ),
         'singular_name' => __( 'Section' ),
         'add_new_item' => 'Add New Section',
         'edit_item' => 'Edit Section',
         'new_item' => 'New Section',
         'search_items' => 'Search Section',
         'not_found' => 'No Sections found',
         'not_found_in_trash' => 'No Sections found in trash',
         'all_items' => __( 'All Sections' ),
       ),
       'query_var' => true,
       'rewrite' => array( 'slug' => 'section' ),
       ));
     if (!get_option('yoursite-static-content-initialized')) {
       $terms = array(
         'Footer',
         'Header',
         'Front Page Intro',
         'Front Page Content',
         );
       foreach($terms as $term) {
         if (!get_term_by('name',$term,'section')) {
           wp_insert_term($term, 'section');
         }
       }
       update_option('yoursite-static-content-initialized',true);
     }
   }
   function manage_static_content_posts_columns($columns){
     $new = array();
     foreach($columns as $key => $title) {
       if ($key=='author') // Put the Sections column before the Author column
         $new['sections'] = 'Sections';
       $new[$key] = $title;
     }
     return $new;
   }
   function manage_posts_custom_column( $column,$post_id ) {
     global $typenow;
     if ($typenow=='static_content') {
       $taxonomy = 'section';
       switch ($column) {
       case 'sections':
         $sections = get_the_terms($post_id,$taxonomy);
         if (is_array($sections)) {
           foreach($sections as $key => $section) {
             $edit_link = get_term_link($section,$taxonomy);
             $sections[$key] = '<a href="'.$edit_link.'">' . $section->name . '</a>';
           }
           echo implode(' | ',$sections);
         }
         break;
       }
     }
   }
   function parse_query($query) {
     global $pagenow;
     $qv = &$query->query_vars;
     if ($pagenow=='edit.php' &&
         isset($qv['taxonomy']) && $qv['taxonomy']=='section' &&
         isset($qv['term']) && is_numeric($qv['term'])) {
       $term = get_term_by('id',$qv['term'],'section');
       $qv['term'] = $term->slug;
     }
   }
   function restrict_manage_posts() {
     global $typenow;
     global $wp_query;
     if ($typenow=='static_content') {
       $taxonomy = 'section';
       $section = get_taxonomy($taxonomy);
       wp_dropdown_categories(array(
         'show_option_all' =>  __("Show All {$section->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_StaticContent::on_load();
}
share|improve this answer

Look I did this (and it worked):

What's the difference (which one is better code)?

I tried this (see the last function):**

    /**
     * Description: Adds a taxonomy filter in the admin list page for a custom post type.
     * Written for: http://wordpress.stackexchange.com/posts/582/
     * By: Mike Schinkel - http://mikeschinkel.com/custom-workpress-plugins
     * Instructions: Put this code in your theme's functions.php file or inside your own plugin. Edit to suite your post types and taxonomies. Hope this helps...
    */
    add_filter('manage_listing_posts_columns', 'add_businesses_column_to_listing_list');
    function add_businesses_column_to_listing_list( $posts_columns ) {
        if (!isset($posts_columns['author'])) {
            $new_posts_columns = $posts_columns;
        } else {
            $new_posts_columns = array();
            $index = 0;
            foreach($posts_columns as $key => $posts_column) {
                if ($key=='author') {
                $new_posts_columns['businesses'] = null;
                }
                $new_posts_columns[$key] = $posts_column;
            }
        }
        $new_posts_columns['businesses'] = 'Businesses';
        return $new_posts_columns;
    }

    add_action('manage_posts_custom_column', 'show_businesses_column_for_listing_list',10,2);
    function show_businesses_column_for_listing_list( $column_id,$post_id ) {
        global $typenow;
        if ($typenow=='listing') {
            $taxonomy = 'business';
            switch ($column_id) {
            case 'businesses':
                $businesses = get_the_terms($post_id,$taxonomy);
                if (is_array($businesses)) {
                    foreach($businesses as $key => $business) {
                        $edit_link = get_term_link($business,$taxonomy);
                        $businesses[$key] = '<a href="'.$edit_link.'">' . $business->name . '</a>';
                    }
                    echo implode(' | ',$businesses);
                }
                break;
            }
        }
    }

    /* JUST AN HYPOTHETICAL EXAMPLE
    add_action('manage_posts_custom_column', 'manage_posts_custom_column',10,2);
    function manage_posts_custom_column( $column_id,$post_id ) {
        global $typenow;
        switch ("{$typenow}:{$column_id}") {
        case 'listing:business':
            echo '...whatever...';
            break;
        case 'listing:property':
            echo '...whatever...';
            break;
        case 'agent:listing':
            echo '...whatever...';
            break;
        }
    }
    */

    add_action('restrict_manage_posts','restrict_listings_by_business');
    function restrict_listings_by_business() {
        global $typenow;
        global $wp_query;
        if ($typenow=='listing') {
            $taxonomy = 'business';
            $business_taxonomy = get_taxonomy($taxonomy);
            wp_dropdown_categories(array(
                'show_option_all' =>  __("Show All {$business_taxonomy->label}"),
                'taxonomy'        =>  $taxonomy,
                'name'            =>  'business',
                '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
            ));
        }
    }

    add_filter('parse_query','convert_business_id_to_taxonomy_term_in_query');
    function convert_business_id_to_taxonomy_term_in_query($query) {
        global $pagenow;
        $qv = &$query->query_vars;
        if ($pagenow=='edit.php' &&
                isset($qv['taxonomy']) && $qv['taxonomy']=='business' &&
                isset($qv['term']) && is_numeric($qv['term'])) {
            $term = get_term_by('id',$qv['term'],'business');
            $qv['term'] = $term->slug;
        }
    }

    add_action('init','register_listing_post_type');
    function register_listing_post_type() {
        register_post_type('listing',array(
            'label' => 'Listings',
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
        ));
    }

    add_action('init','register_business_taxonomy');
    function register_business_taxonomy() {
        register_taxonomy('business',array('listing'),array(
            'label' => 'Businesses',
            'public'=>true,
            'hierarchical'=>true,
            'show_ui'=>true,
            'query_var'=>true
        ));
    }

    add_action('init','insert_default_terms');
    function insert_default_terms() {
      if (!get_option('site-page-content-initialized')) {
        $terms = array(
          'Intro (Front Page)',
          'Content (Front Page)',
          'Slider (Front Page)',
          );
        foreach($terms as $term) {
          if (!get_term_by('name',$term,'business')) {
            wp_insert_term($term, 'business');
          }
        }
        update_option('site-page-content-initialized',true);
      }
    }
share|improve this answer
    
The one I gave you encapsulates everything into a self-contained package, so I would say it is probably better. But then this uses business listings, not your static content. Were you just trying to get something to work and not concerned about what it was? –  MikeSchinkel Jan 24 '11 at 9:06
    
@MikeSchinkel OK, I got it thanks again. Yes, I just wanted the whole thing to work, and change the name of the custom posts and taxonomies later. –  janoChen Jan 24 '11 at 9:12

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.