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

wp_dropdown_categories dosn't work with Custom Post Type/Taxonomy WordPress

  • SOLVED

I am wanting to be able to select at custom taxonomy category from drop down list and then list the post (custom type) for that category using wp_dropdown_categories.

Here is a [[LINK href="http://shenandoahvalleylodging.com/search/"]]link [[/LINK]]to a test site showing example. Notice the wp_list_categories displays correct and and clicking on the link takes you to the proper page. But in the drop down, it displays the correct categories but when you click on and submit, you get a 404 page, as the URL does not appear to be valid.

I have tried different values below and still get 404 after submiting selection.


$taxonomy = 'jobs_category';
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'hierarchical' => $hierarchical,
'title_li' => $title
);
?>

<ul>
<?php wp_list_categories( $args ); ?>
</ul>


<h2><?php _e('Categories:'); ?></h2>
<form action="<?php bloginfo('url'); ?>" method="get">
<div>
<?php wp_dropdown_categories($args); ?>
<input type="submit" name="submit" value="view" />
</div>
</form>


########################################################
Here is the Custom Post type and taxonomy:


add_action('init', 'jobs_register');

function jobs_register() {

$labels = array(
'name' => _x('Job Postings', 'hes'),
'singular_name' => _x('Job Posting', 'hes'),
'add_new' => _x('Add New', 'hes'),
'add_new_item' => __('Add New Job'),
'edit_item' => __('Edit Job'),
'new_item' => __('New Job'),
'view_item' => __('View Job'),
'search_items' => __('Search Job'),
'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,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array ('slug' => 'jobs'),
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail')
);

register_post_type( 'jobs' , $args );
}



add_action( 'init', 'jobs_taxonomies' );

function jobs_taxonomies() {

register_taxonomy(
'jobs_category',
array( 'jobs' ),
array(
'hierarchical' => true,
'labels' => array(
'name' => 'Job Categories',
'singular_name' => 'Job Category',
),
'rewrite' => array(
'slug' => 'job-categories',
'with_front' => false
),
)
);
}



Answers (4)

2011-10-26

Luis Abarca answers:

Try this way http://shenandoahvalleylodging.com/?term_id=3&submit=view

With term_id


69developer comments:

Where do you change that at?


Luis Abarca comments:



$args = array(
<strong>
'name' => 'term_id',
'id' => 'term_id',
</strong>
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'hierarchical' => $hierarchical,
'title_li' => $title
);


69developer comments:

I did that and it now displays all the post, not the category that was chosen.


Luis Abarca comments:

Try to go to Settings > Permalinks to flush the rewrite rules and try again.


Luis Abarca comments:

You can also use this code.


<?php
$taxonomy = 'jobs_category';
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'hierarchical' => $hierarchical,
'title_li' => $title
);
?>

<h2><?php _e('Categories:'); ?></h2>
<form action="<?php bloginfo('url'); ?>" method="get">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories">
<?php foreach ($cats as $cat) : ?>
<option value="<?php echo get_term_link($cat, $cat->taxonomy) ?>"><?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="view" />
</div>
</form>

<script>
jQuery(document).ready(function()
{
jQuery('#categories').change(function()
{
window.location = jQuery(this).val();
});
});
</script>


Luis Abarca comments:

Updated: I change to perform the action on button click


<script>
jQuery(document).ready(function()
{
jQuery('#submit').click(function()
{
window.location = jQuery('#categories').val();
return false;
});
});

</script>


69developer comments:

So I will have to include jquery with this?


69developer comments:

I put the new code in, but it displays all the post not just the ones in the category.


Luis Abarca comments:

<strong>
First, try to go to Settings > Permalinks first and try the way of Ivalo and Jurre Hanema says.
</strong>

Yes, If you use like i said, you need jQuery, just add it in functions.php

<?php
function add_jQuery()
{
wp_enqueue_script('jquery');
}

add_action('wp_enqueue_scripts', 'add_jQuery');
?>


69developer comments:

I went to Permalinks, saved. Did that a few times. But it still displays all posts with your code above.


69developer comments:

I did add jquery, and it worked with the one category (auto loaded the page) but when I click 'View' it still displays all posts.


Luis Abarca comments:

Yep you need to change the script for the updated version, check this file

http://pastebin.com/XgiXLg5C


69developer comments:

I just pasted the code pastbin.

It still wont work.


Luis Abarca comments:

Sorry, my bad, i was checking for "click" instead of "submit" event :(

If you change the type="submit" to type="button" it wil work, but this is the updated version:
http://pastebin.com/XgiXLg5C


69developer comments:

[[LINK href="http://shenandoahvalleylodging.com/search/"]]Link[[/LINK]]
I copied that and it still not working.

Here is what I have:

$taxonomy = 'jobs_category';
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'hierarchical' => $hierarchical,
'title_li' => $title
);
?>

<ul>
<?php wp_list_categories( $args ); ?>
</ul>

<h2><?php _e('Categories:'); ?></h2>
<form action="<?php bloginfo('url'); ?>" method="get">

<div>
<?php $cats = get_categories($args); ?>
<select id="categories">
<?php foreach ($cats as $cat) : ?>
<option value="<?php echo get_term_link($cat, $cat->taxonomy) ?>"><?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="view" />
</div>
</form>

<script>
jQuery(document).ready(function()
{
jQuery('#submit').submit(function()
{
window.location = jQuery('#categories').val();
return false;
});
});
</script>


Luis Abarca comments:

OK, i was wrong with the jQuery, this is OK, i tested and its runing http://pastebin.com/XgiXLg5C


69developer comments:

That works!!!

So I guess the wp_dropdown_categories dosnt work well with custom post types?


It works with the click 'View', anyway to have it work like you had it with auto load after change of selection and the current way?


Thanks!!!!!


Luis Abarca comments:

You are welcome amigo!, let me know if you have any issue

2011-10-26

rizaljohn answers:

I guess it's because your url parameter is 'cat'. Try to replace it with 'category'.

2011-10-26

Jurre Hanema answers:

Try modifying the arguments to this:



$args = array(
'taxonomy' => $taxonomy,
'name' => 'job-categories',
'orderby' => $orderby,
'show_count' => $show_count,
'hierarchical' => $hierarchical,
'title_li' => $title
);


69developer comments:

That did modify the URL but I still get a 404.

2011-10-26

Ivaylo Draganov answers:

Hi,

it's querying the wrong thing. Pass it a 'name' parameter the same as 'taxonomy', like so:

$args = array(
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'hierarchical' => $hierarchical,
'title_li' => $title

);


69developer comments:

This the URL that it produced: http://shenandoahvalleylodging.com/?jobs_category=3&submit=view

But I get a 404.


Ivaylo Draganov comments:

I didn't notice that you've changed the rewrite slug. Try passing the rewrite slug as 'name' and it should work without jQuery:

'name' => 'job-categories'