Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom admin post lists no longer working in 3.1 WordPress

  • SOLVED

I put together some code that adds filters and columns of a custom taxonomy to the admin lists of a custom post type. It worked really well, however it has stopped working and from what I can tell it's since upgrading to 3.1. I expect something in the core has changed to stop this functioning, but I don't know what it is!

//Filter by Client

add_filter('manage_job_posts_columns', 'add_clients_column_to_job_list');
function add_clients_column_to_job_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['clients'] = null;
}
$new_posts_columns[$key] = $posts_column;
}
}
$new_posts_columns['clients'] = 'Client';
return $new_posts_columns;
}

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


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

Answers (3)

2011-03-09

Oleg Butuzov answers:

actually it works...


what you see columsn or extra select? or none?


Oleg Butuzov comments:

can be a small problem if you have hierarhial posts in that case you need to use

manage_pages_custom_column filter


James Beardmore comments:

Sorry I probably wasn't clear enough. The columns and filters appear but when you use the filters they always return no posts even when there are several.


Oleg Butuzov comments:

you don't see any post whan you use a select with aclient taxonomy right?


James Beardmore comments:

Exactly. If I have 7 posts that have a client of "foo", if I use the filter to just show foo, I get no results.


Oleg Butuzov comments:

give me access to the your host. i know where is the issue


Oleg Butuzov comments:

or just add this

/***************************************************************************************/
add_action('restrict_manage_posts', 'restrict_manage_posts_client');
function restrict_manage_posts_client(){
global $post_type;

if ( is_object_in_taxonomy( $post_type, 'client' ) ) {
$dropdown_options = array(
'show_option_all' => __( 'View all categories' ),
'hide_empty' => 0,
'hierarchical' => 1,
'name' => 'client',
'show_count' => 0,
'taxonomy' => 'client',
'orderby' => 'name',
'selected' => $cat
);

add_filter('wp_dropdown_cats', 'wp_dropdown_cats_filter', 10);
wp_dropdown_categories( $dropdown_options );
remove_filter('wp_dropdown_cats', 'wp_dropdown_cats_filter', 10);
}
}


function wp_dropdown_cats_filter($select){
$terms = get_terms('client', array('hide_empty' => false));
foreach($terms as $term){
$select = str_replace('value="'.$term->term_id.'"', 'value="'.$term->slug.'"', $select);
if (isset($_GET['client']) && $term->slug == $_GET['client']){
$select = str_replace('value="'.$term->slug.'"', 'value="'.$term->slug.'" selected', $select);
}
}
return $select;
}


your restrict_manage_posts you can remove temporary


James Beardmore comments:

A new problem. Works great for one taxonomy, but I can't use it to add filters for multiple taxonomies, it breaks. How can I go about this?

2011-03-09

Sébastien | French WordpressDesigner answers:

could you paste the code of custom_post_type and taxonomy please, i look at this in my site of test

2011-03-09

Daniele Raimondi answers:

I'm using the same technique on my last WP site (3.1), and i think your problem is in the "wp_dropdown_categories" output: "wp_dropdown_categories" creates a select and add options to it using the term ids as values instead of the term slugs.
Try to create the dropdown on your own. For example:

//Add a select to filter ANNOUNCEMENTS by custom tax TYPE
add_action('restrict_manage_posts','restrict_announcement_by_type');
function restrict_announcement_by_type() {
global $typenow;
global $wp_query;
if ($typenow=='announcement') {
// 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('type');
foreach ($filters as $tax_slug) {
// retrieve the taxonomy object
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
// retrieve array of term objects per taxonomy
$terms = get_terms($tax_slug);

// output html for taxonomy dropdown filter
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
echo "<option value=''>".__("Show All ").$tax_name."</option>";
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, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
}
echo "</select>";
}
}
}



(sorry for my english)
Thanks, Daniele.