5

Hello I'm wondering if anyone can help me figure this out. Essentially when I click on my custom post type 'Portfolio' in admin, I see a list of 'Services' next to posts, but when I click on one to filter (e.g. http://localhost:8888/wp-admin/edit.php?post_type=portfolio&Service=web-design) nothing happens! It still lists all taxonomies.

Below is code used to register custom post type & taxonomy (apologies for length, but can't work out what is wrong):

add_action('init','portfolio_register');

function portfolio_register(){

$labels=array(
'name'=> _x('Portfolio','posttype general name'),
'singular_name'=> _x('Project','posttype singular name'),
'add_new'=> _x('Add New Project','portfolio item'),
'add_new_item'=> __('Add New Project'),
'edit_item'=> __('Edit Project'),
'new_item'=> __('New Project'),
'view_item'=> __('View Project'),
'search_items'=> __('Search Portfolio'),
'not_found'=>  __('Nothing found'),
'not_found_in_trash'=> __('Nothing found in Trash'),
'parent_item_colon'=>'');

$args=array(
'labels'=>$labels,
'public'=>true,
'publicly_queryable'=>true,
'show_ui'=>true,
'show_in_menu'=>true,
'menu_position'=>5,
'query_var'=>true,
'menu_icon'=> get_stylesheet_directory_uri().'/images/mag.png',
'rewrite'=>true,'capability_type'=>'post',
'hierarchical'=>false,
'supports'=>array('title','editor','thumbnail','comments')); 

register_post_type('portfolio',$args);}

register_taxonomy("Service",array("portfolio"),array("hierarchical"=>true,"label"=>"Service","singular_label"=>"Service","rewrite"=>true));

Then to view columns in admin:

add_filter( 'manage_edit-portfolio_columns', 'my_edit_portfolio_columns' ) ;

function my_edit_portfolio_columns( $columns ) {

    $columns = array(
        'cb' => '<input type="checkbox" />',
        'title' => __( 'Project' ),
        'Service' => __( 'Services' ),
        'date' => __( 'Date' )
    );

    return $columns;
}

add_action( 'manage_portfolio_posts_custom_column', 'my_manage_portfolio_columns', 10, 2 );

function my_manage_portfolio_columns( $column, $post_id ) {
    global $post;

    switch( $column ) {


        /* If displaying the 'service' column. */
        case 'Service' :

            /* Get the genres for the post. */
            $terms = get_the_terms( $post_id, 'Service' );

            /* If terms were found. */
            if ( !empty( $terms ) ) {

                $out = array();

                /* Loop through each term, linking to the 'edit posts' page for the specific term. */
                foreach ( $terms as $term ) {
                    $out[] = sprintf( '<a href="%s">%s</a>',
                        esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'Service' => $term->slug ), 'edit.php' ) ),
                        esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'Service', 'display' ) )
                    );
                }

                /* Join the terms, separating them with a comma. */
                echo join( ', ', $out );
            }

            /* If no terms were found, output a default message. */
            else {
                _e( 'No Services' );
            }

            break;

        /* Just break out of the switch statement for everything else. */
        default :
            break;
    }
}

1 Answer 1

5

Basically you are doing every thing right except the taxonomy slug in the url should be lowercase so you can simply place this line:

esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'Service' => $term->slug ), 'edit.php' ) ),

with this:

esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'service' => $term->slug ), 'edit.php' ) ),

and you should be fine.

And here is a bonus to get a dropdown as a filter as well: enter image description here

// Adding a Taxonomy Filter to Admin List for a Custom Post Type
add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
function my_restrict_manage_posts() {

    // only display these taxonomy filters on desired custom post_type listings
    global $typenow;
    if ($typenow == 'portfolio') {

        // create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
        $filters = array('Service');

        foreach ($filters as $tax_slug) {
            // retrieve the taxonomy object
            $tax_obj = get_taxonomy($tax_slug);
            $tax_name = $tax_obj->labels->name;

            // output html for taxonomy dropdown filter
            echo "<select name='".strtolower($tax_slug)."' id='".strtolower($tax_slug)."' class='postform'>";
            echo "<option value=''>Show All $tax_name</option>";
            generate_taxonomy_options($tax_slug,0,0,(isset($_GET[strtolower($tax_slug)])? $_GET[strtolower($tax_slug)] : null));
            echo "</select>";
        }
    }
}

function generate_taxonomy_options($tax_slug, $parent = '', $level = 0,$selected = null) {
    $args = array('show_empty' => 1);
    if(!is_null($parent)) {
        $args = array('parent' => $parent);
    }
    $terms = get_terms($tax_slug,$args);
    $tab='';
    for($i=0;$i<$level;$i++){
        $tab.='--';
    }
    foreach ($terms as $term) {
        // output each select option line, check against the last $_GET to show the current option selected
        echo '<option value='. $term->slug, $selected == $term->slug ? ' selected="selected"' : '','>' .$tab. $term->name .' (' . $term->count .')</option>';
        generate_taxonomy_options($tax_slug, $term->term_id, $level+1,$selected);
    }

}
2
  • Brilliant! Thank you so much for taking the time to help, I've spent ages trying to figure this out. Also thanks for the dropdown filter - that makes it much easier to sort.
    – vonholmes
    Dec 19, 2012 at 17:43
  • Glad i Could help :)
    – Bainternet
    Dec 19, 2012 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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