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

Custom posts types & categories in wp_dropdown_categories WordPress

  • SOLVED

In my search.php, I am filtering my results with a dropdown.

This all works fine, however I need to display two custom post types in the dropdown as well as the categories. These custom post type id's are 'reading' and 'video'.

<strong>Dropdown</strong>

<?php wp_dropdown_categories('&show_option_none=All&exclude=1,16,8&hide_empty=0'); ?>

<strong>Form</strong>
<form class="search-page-form" action="/">
<h4 class="meta-heading">Advanced Search</h4>
<div class="clearfix">
<fieldset>
<label>Keywords</label>
<input type="text" class="search form-text" name="s" id="s" value="<?php echo trim( get_search_query() ); ?>" placeholder="Type keywords here" />
</fieldset>
<fieldset>
<label>Category</label>
<div class="styled-select">
<div class="icon"></div>
<?php wp_dropdown_categories('&show_option_none=All&exclude=1,16,8&hide_empty=0'); ?>
</div>
</fieldset>
<fieldset>
<label>Sector Filter</label>
<div class="styled-select">
<div class="icon"></div>
<? wp_dropdown_categories(array('taxonomy'=> 'post_tag','hide_empty' => 0, 'name' => 'my_tags', 'exclude' => '14,4', 'show_option_none' => 'All')); ?>
</div>
</fieldset>
</div>
<fieldset class="submit">
<input type="submit" class="submit button btn-blue" name="submit" id="searchsubmit" value="Search"/>
<a href="/search/" class="reset-link">Reset</a>
</fieldset>

</form>

Answers (3)

2012-08-21

Pavel Petrov answers:

Do you mean something like this?

function dd_categories($exclude) {
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => $exclude,
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false );

$categories = get_categories( $args );
$menu = '<select name="cat" id="cat" class="postform">';
$menu .= '<option value="-1">All</option>';
foreach($categories as $category)
{
if($category->name !== 'Uncategorized' && $category->name !== 'Blogroll')
{
$menu .= '<option class="level-0" value="'.$category->term_id.'">'.$category->name.'</option>';
}
}
$menu .= '<option class="level-0" value="reading">Reading</option>';
$menu .= '<option class="level-0" value="video">Video</option>';
$menu .= '</select>';
echo $menu;
}
dd_categories('1,16,8');


Nick comments:

Wow thanks, almost there only thing is with your code the search via a category

http://wellsaid/?s=the&cat=video&submit=Search

When the custom post is post_type=video, any ideas? So it is searching by a category as opposed to a post_type.


Pavel Petrov comments:

Ok lets change that a little bit to see if it works:


function dd_categories($exclude) {

$args = array(

'type' => 'post',

'child_of' => 0,

'parent' => '',

'orderby' => 'name',

'order' => 'ASC',

'hide_empty' => 0,

'hierarchical' => 1,

'exclude' => $exclude,

'include' => '',

'number' => '',

'taxonomy' => 'category',

'pad_counts' => false );



$categories = get_categories( $args );

$menu = '<select name="post_type" id="post_type" class="postform">';

$menu .= '<option value="-1">All</option>';

foreach($categories as $category)

{

if($category->name !== 'Uncategorized' && $category->name !== 'Blogroll')

{

$menu .= '<option class="level-0" value="'.$category->term_id.'">'.$category->name.'</option>';

}

}

$menu .= '<option class="level-0" value="reading">Reading</option>';

$menu .= '<option class="level-0" value="video">Video</option>';

$menu .= '</select>';

echo $menu;

}

dd_categories('1,16,8');

In your functions.php paste:

function SearchFilter($query) {
if ($query->is_search or $query->is_feed) {
if(is_numeric($_GET['post_type'])) {
$query->set('cat', $_GET['post_type']);
}
else{
$query->set('post_type',$_GET['post_type']);
}
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

Keep in mind that I`ve written here, so it hasn`t been tested I`ll test in a min


Nick comments:

Thanks you that works great!


Pavel Petrov comments:

Cool :]


Nick comments:

How do pay you? (Sorry noob here)


Pavel Petrov comments:

I have no idea, I`m only answering :). I guess you have to choose my answer for correct and you`ll probably get a mail.

2012-08-21

Abdelhadi Touil answers:

Hi.
I think this may help you:
[[LINK href="http://www.aroundwp.com/search-form-with-custom-taxonomy-terms-and-categories/"]]http://www.aroundwp.com/search-form-with-custom-taxonomy-terms-and-categories/[[/LINK]]
Good luck.


Nick comments:

I'm trying to combine two custom posts types and categories into one dropdown, this tutorial uses two dropdowns.

2012-08-22

Daniel answers:

Make sure you have your PayPal email address in your Account settings too!