Custom taxonomy drop down

Here is a quick example that will help you create a drop down menu, populated with all of the terms from a custom taxonomy.

This code is mostly from this thread on WordPress.com forums, I’ve just changed a few things for my situation.

Here’s the function that does most of the hard work.

function get_terms_dropdown($taxonomies, $args){
	$myterms = get_terms($taxonomies, $args);
	$output ="<select name='buscategory'>";
	$output .="<option value='#'>Please select a business category</option>";
	foreach($myterms as $term){
		$root_url = get_bloginfo('url');
		$term_taxonomy=$term->taxonomy;
		$term_slug=$term->slug;
		$term_name =$term->name;
		$link = $term_slug;
		$output .="<option value='".$link."'>".$term_name."</option>";
	}
	$output .="</select>";
return $output;
}

Then place this code where you would like to call the drop down menu, this could be using a hook or in your template files.

<form action="<?php bloginfo('url'); ?>" method="get">
<p>Or search for a service from the drop down menu:</p>
<?php
$taxonomies = array('buscategory');
$args = array('orderby'=>'name','hide_empty'=>true);
$select = get_terms_dropdown($taxonomies, $args);

$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
	<noscript><div><input type="submit" value="Go" /></div></noscript>
	</form>
	</div>

You’ll need to update the name of the taxonomy for this to work, you’ll see in the above example I’ve used ‘buscategory’ it needs to be update in both code blocks.

WordPress search: only show post and not pages

This tip is a quick one but something asked for regularly. By default the WordPress will show both posts and pages in the results.

If you’d rather just show posts and you use the Thesis framework, just place the code below in your custom_functions.php file and you’re done!

<?php
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>

Here’s an updated version of this that fixes the search when you’re in the administration screen, basically leaving that to it’s default behaviour when your logged in.

The other thing I’ve added to this is the ability to place a hidden form field in a search form then use this if statement to test if we want to search just with in a custom post type, in this case one called organisations.

function SearchFilter($query) {
if ($query->is_search && !is_admin()) {

	// are we wanting to search Custom posts?
	if ($_GET['bsearch'] =="yes"){
			$query->set('post_type', 'organisations');
	}
	else{
			//default behavour we want is to search posts only
			$query->set('post_type', 'post');
	}

}
return $query;
}

add_filter('pre_get_posts','SearchFilter');

How to create a WordPress Custom Post Type

Since learning about Custom Post Types in WordPress 3.x I seem to be using them in just about every WordPress build we do.

The code below is what I’ve been using to get register a Custom Post Type, it’s pretty straight forward, now that I know what’s going on with it.

If you want the full run down this post on Justin Tadlock’s web site is definitely the place to start.

Anyhow, if you’re using Thesis, then this code place in your custom_function.php file will get you a new post type, in the example here for Case Studies.


// code to create Custom post type for Case Studies
function create_my_post_types() {
register_post_type( 'Case_Studies',
array(
'labels' => array(
'name' => __( 'Case_Studies' ),
'singular_name' => __( 'Case_Study' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Case Study' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Case Studies' ),
'new_item' => __( 'New Case Study' ),
'view' => __( 'View Case Studies' ),
'view_item' => __( 'View Case Study' ),
'search_items' => __( 'Search Case Studies' ),
'not_found' => __( 'No Case Studies found' ),
'not_found_in_trash' => __( 'No Case Studies found in Trash' )

),
'public' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'supports' => array( 'title', 'editor','excerpt', 'custom-fields'),
)
);
}
add_action( 'init', 'create_my_post_types' );

When you’ve registered your custom post types, create a new entry with the new post type, then you will need to do one more thing before it will work. You have to visit the Permalinks section of your WordPress admin before the permalinks for your Custom Post Type will be recognised on the front end of your site.

Simply visit the admin page, that’s all and it will start working when you view a post of your custom post type.