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

Customization of custom post admin screen filter drop downs WordPress

I have put together a function that adds a drop down taxonomy filter to the admin screen of a custom post type "job". This drop down shows a post count, however I need this post count to only show the published jobs in the taxonomy, not all the jobs. Is there a simple way to do this without rewriting everything?

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 a select to filter by custom tax TYPE

add_action('restrict_manage_posts','restrict_job_by_client');

function restrict_job_by_client() {
global $typenow;
global $wp_query;
if ($typenow=='job') {
// 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('client');
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>";
}
}
}

Answers (2)

2011-04-26

Peter Michael answers:

[[LINK href="http://wordpress.org/support/topic/custom-taxonomy-post-count-incorrect?replies=9"]]http://wordpress.org/support/topic/custom-taxonomy-post-count-incorrect?replies=9[[/LINK]]


James Beardmore comments:

Unfortunately that doesn't fix it.


Peter Michael comments:

Try setting the returned fields to all:

$terms = get_terms( $tax_slug, 'get=all&hide_empty=1');


James Beardmore comments:

No, that's not working either.


Peter Michael comments:

One more:

Can you add this to your register_taxonomy function:


'update_count_callback' => _update_post_term_count,


See [[LINK href="http://codex.wordpress.org/Function_Reference/register_taxonomy"]]http://codex.wordpress.org/Function_Reference/register_taxonomy[[/LINK]], section 'update_count_callback'


Peter Michael comments:

Just tested, works for me.


James Beardmore comments:

Added update count callback to the register taxonomy function, this had an effect, but the counts are still wrong.

There are currently 173 posts, 170 published, 1 draft, 2 scheduled. also 112 in the trash.

The post counts should only be showing the published posts and they add up to more than 170 every time.


Peter Michael comments:

Can you manually update the count in the DB and check if it counts correctly afterwards? I made my tests with a new taxonomy and the counts were always correct. Also would like to know how much 'more' than 170, i.e. the total count you get. Furthermore I'm assuming you're using the latest version of WP (3.1.2)

Might be better if you could post your register_taxonomy function here.


Peter Michael comments:

Any updates?

2011-04-29

AdamGold answers:

Try to add this code to functions.php, it will make your get_terms function to filter out the unpublished posts from it:
function get_terms_filter( $terms, $taxonomies, $args )
{
global $wpdb;
$taxonomy = $taxonomies[0];
if ( ! is_array($terms) && count($terms) < 1 )
return $terms;
$filtered_terms = array();
foreach ( $terms as $term )
{
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND p.post_status = 'publish' LIMIT 1");
if ( intval($result) > 0 )
$filtered_terms[] = $term;
}
return $filtered_terms;
}
add_filter('get_terms', 'get_terms_filter', 10, 3);


<em>Source: http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/</em>