2

I'm using get_the_term_list and manage_posts_custom_column to display the terms of a custom taxonomy in the list of custom posts, in the WordPress Admin.

add_action( 'manage_mycustompost_posts_custom_column' , 'custom_mycustompost_column', 10, 2 );

function custom_mycustompost_column( $column, $post_id ) {
  switch ( $column ) {
    case 'category_names':
        echo get_the_term_list( $post_id , 'mycustomcategory' , '' , ',' , '' );
        break;
  }
}

However, for each taxonomy term, I get a link to the public page that shows posts for that term: http://www.myblog.com/mycustomcategory/test/

I would like to get a link that filters the list in the WordPress Admin. I would like a link like: http://www.myblog.com/wp-admin/edit.php?post_type=post&category_name=news

The type of link I get in the Posts list, for tags or categories of the default WordPress posts.

Is there a WordPress function that does that?

2

It doesn't have it wrapped inside a function you can use, but the following will work:

$taxonomy = 'my-taxonomy';
$taxonomy_object = get_taxonomy( $taxonomy );
$object_type = get_post_type($post_id);

if ( $terms = get_the_terms( $post_id, $taxonomy ) ) {
      $out = array();
      foreach ( $terms as $t ) {
           $posts_in_term_qv = array();
           $posts_in_term_qv['post_type'] = $object_type;

           if ( $taxonomy_object->query_var ) {
                 $posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug;
           } else {
                 $posts_in_term_qv['taxonomy'] = $taxonomy;
                 $posts_in_term_qv['term'] = $t->slug;
           }

           $out[] = sprintf( '<a href="%s">%s</a>',
                    esc_url( add_query_arg( $posts_in_term_qv, 'edit.php' ) ),
                    esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
                 );
        }

      /* translators: used between list items, there is a space after the comma */
       echo join( __( ', ' ), $out );
};
  • Thank you for your answer. I was hoping for an easier, built-in way, especially as WordPress uses this functionality for the default posts list... – Alex Vang Nov 16 '12 at 18:51
  • Nope :). Actually that was pretty much lifted out of WP 3.5 core (see this trac ticket: core.trac.wordpress.org/ticket/21240 ) – Stephen Harris Nov 16 '12 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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