Skip to content

Instantly share code, notes, and snippets.

@xposedbones
Created December 12, 2013 21:27
Show Gist options
  • Save xposedbones/7935765 to your computer and use it in GitHub Desktop.
Save xposedbones/7935765 to your computer and use it in GitHub Desktop.
filter by custom taxonomy and/or custom post type in the wordpress backend
<?php
add_action('restrict_manage_posts','create_lists');
function create_lists() {
global $typenow;
$filters_arr = array();
// In which page do you want the filters to display,
// $filters_arr = array("emploi");
if(in_array($typenow, $filters_arr)){
$filters = array();
//Custom Taxonomy sort , put each taxonomy that you want listed separated by a comma
//$filters = array("Custom taxonomy 1", "Custom taxonomy 2", "etc");
foreach($filters as $tax_slug){
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
$terms = get_terms($tax_slug);
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
echo "<option value=''>View all $tax_name</option>";
foreach($terms as $term){
echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .'</option>';
}
echo "</select>";
}
list_cpt("recruteurs");
}
}
function list_cpt($pt){
$get = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => $pt,
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => "ASC",
'supress_filters' => 0 )
);
//get cpt info
$info = get_post_type_object( $pt );
$cpt_name = $info->labels->name;
echo "<select name='cpt' id='".$pt."' class='postform'>";
echo "<option value=''>View all ".$cpt_name."</option>";
//print_r($get);
foreach($get->posts as $option){
//echo "<option value='".$option->ID, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>'.$option->post_title."</option>";
echo '<option value='. $option->ID, $_GET['cpt'] == $option->ID ? ' selected="selected"' : '','>' . $option->post_title .'</option>';
}
echo "</select>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment