Ask your WordPress questions! Pay money and get answers fast! (more info)

Dropdown in custom post-type list to filter taxonomy + showcount WordPress

  • SOLVED

Hello,

I'm after a function please to simply do exactly what the category drop down does in posts.

I need the dropdown filter to appear here... [[LINK href="http://i.imgur.com/rlmP1.png"]]http://i.imgur.com/rlmP1.png[[/LINK]]

and the dropdown filter to appear in multiple post-types...

<strong>on-road</strong>
<strong>off-road</strong>


And the taxonomy is called... <strong>year</strong>


This is what I got to but 'show_count' is got from all both post-types, and the filter doesnt actually work. Plus it only shows in on-road.

Can any one please help, money goes to fully working function.




// CUSTOM TAXONOMY FOR YEAR- AS A DROP DOWN IN POST-TYPE on-road AND off-road
function pippin_add_taxonomy_filters() {
global $typenow;

// an array of all the taxonomyies you want to display. Use the taxonomy name or slug
$taxonomies = array('year');

// must set this to the post type you want the filter(s) displayed on
if( $typenow == 'on-road' ){

foreach ($taxonomies as $tax_slug) {
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
$terms = get_terms($tax_slug);
if(count($terms) > 0) {
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
echo "<option value=''>Show All $tax_name</option>";
foreach ($terms as $term) {
echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
}
echo "</select>";
}
}
}
}
add_action( 'restrict_manage_posts', 'pippin_add_taxonomy_filters' );






Thanks
Josh

Answers (1)

2012-11-07

Maor Barazany answers:

'year' is a WP reserved term, and you should not use it, to avoid unexpected behaviour.
[[LINK href="http://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms"]]http://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms[[/LINK]]

First change your taxonomy name (you may need to reassign all terms to the new taxonomy), then re-save your peramlink structrue, update your function to use the new taxonomy name, and then check.
I had similar issue with 'year' taxonomy, changing the name solved all issues.


Josh Cranwell comments:

Sorry for late reply, lots of other stuff going on.

Anyway, thanks for making realise about the reserved terms. I have modified now and seems to be working.

Though I guess the the show count will always count the entirety of posts assigned to that term. So it's impossible to count the posts specifically to on either on-road or off-road.


// CUSTOM TAXONOMY FOR YEAR- AS A DROP DOWN IN POST-TYPE
function pippin_add_taxonomy_filters() {
global $typenow;

// an array of all the taxonomyies you want to display. Use the taxonomy name or slug
$taxonomies = array('model-year');

// must set this to the post type you want the filter(s) displayed on
if( $typenow == ( 'on-road' || 'off-road' )){

foreach ($taxonomies as $tax_slug) {

$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
$terms = get_terms($tax_slug);

if(count($terms) > 0) {

echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
echo "<option value=''>Filter $tax_name</option>";
foreach ($terms as $term) {
echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
}
echo "</select>";

}
}
}
}
add_action( 'restrict_manage_posts', 'pippin_add_taxonomy_filters' );



Thanks
Josh