WordPress.org

Ready to get started?Download WordPress

Forums

Show Categories Filter on Custom Post Type List (15 posts)

  1. Od3n
    Member
    Posted 4 years ago #

    i would like to display categories filter like the normal post in my custom post type list.
    take a look here : http://dl.dropbox.com/u/2317543/Screen%20shot%202010-06-10%20at%203.01.27%20AM.png

  2. dalberts69
    Member
    Posted 4 years ago #

    Hopefully you found your answer, as it took me some serious searching and some combination of code to find this answer, but this is what worked for me:

    add_action('restrict_manage_posts','my_restrict_manage_posts');
    
    		function my_restrict_manage_posts() {
    			global $typenow;
    
    			if ($typenow=='your_custom_post_type'){
                             $args = array(
                                 'show_option_all' => "Show All Categories",
                                 'taxonomy'        => 'your_custom_taxonomy',
                                 'name'               => 'your_custom_taxonomy'
    
                             );
    				wp_dropdown_categories($args);
                            }
    		}
    add_action( 'request', 'my_request' );
    function my_request($request) {
    	if (is_admin() && $GLOBALS['PHP_SELF'] == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type']=='your_custom_post_type') {
    		$request['term'] = get_term($request['your_custom_taxonomy'],'your_custom_taxonomy')->name;
    
    	}
    	return $request;
    }

    Hopefully this helps, and if anyone has suggestions how to improve it I'd be glad to hear it.

  3. viter-z-bayraku
    Member
    Posted 3 years ago #

    Thanks!!

  4. Daniel King
    Member
    Posted 3 years ago #

    Dalbert, you're a genius. I was looking for this fix for some months now.

    I should add a note: if you're using a subfolder for your WP installation, this if statement will not work:
    if (is_admin() && $GLOBALS['PHP_SELF'] == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type']=='your_custom_post_type') {

    ie: my WP it's installed on /test subfolder. so my $GLOBALS['PHP_SELF'] will not be equal to '/wp-admin/edit.php'. It's value will be: '/test/wp-admin/edit.php'
    Therefore, the if statement will fail.

    A quick fix I add to your code:

    function my_request($request) {
        $current_url = substr( $GLOBALS['PHP_SELF'], -18);
        print_r ( $current_url );
        if (is_admin() && $current_url == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type']=='disenadores') {
            $request['term'] = get_term($request['categoria-diseno'],'categoria-diseno')->name;
            echo 'entré';
        }
        return $request;
    }

    hope it helps

    edited for typos

  5. styledev
    Member
    Posted 3 years ago #

    Is there any reason why this would work for some custom post types and not others. I have a total of four custom post types and this works perfect for Events and Success Stories.

    Issues:

    • On Staff Members it throws a "no ... found" page for one term "Head Coach".
    • On WODs I consistently get the "no ... found" page.

    For the Head Coach one I thought maybe since it is made up of two words but the url that is created uses the term ID.

    functions.php - http://pastie.org/private/nymddfgstu8r7eoaslgaq
    post-columns.php (filter code located here) - http://pastie.org/private/yxbtiu3anfmzgexcd5vgsa
    taxonomies.php - http://pastie.org/private/ama35mgctl9ycepfyae5bq
    post-types.php - http://pastie.org/private/zqziceoctvduldopxlirjq

    Thanks

  6. sebmeric
    Member
    Posted 3 years ago #

    This is perfect !

    And you may even add to the $args array the 'selected' param to keep track of the searched term after page reload :

    'selected' => $_GET['your_custom_taxonomy']

    Thanx a lot for this piece of code :)

  7. Daniel King
    Member
    Posted 3 years ago #

  8. sebmeric
    Member
    Posted 3 years ago #

    Thank you. Very interesting tutorial :)

  9. Daniel King
    Member
    Posted 3 years ago #

    I just realized my ammend to dalbert code it's full of echo and stuff.

    Also, the check for current_url part seems to be unnecesary:
    if (is_admin() && $current_url == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type']=='your_custom_post') {

    Here's a cleaner version:

    function my_request($request) {
    if (is_admin() && isset($request['post_type']) && $request['post_type']=='your_custom_post') {
    $request['term'] = get_term($request['your_custom_taxonomy'],'your_custom_taxonomy')->name;
    }
    return $request;
    }

    Did any of you have a better fix than this? Anyone knows of a plugin with manage this?

  10. styledev
    Member
    Posted 3 years ago #

    I have found somatic's method the best solution as it is compatible with the coming version of 3.1.

    http://wordpress.stackexchange.com/questions/578/adding-a-taxonomy-filter-to-admin-list-for-a-custom-post-type/3215#3215

  11. hafman
    Member
    Posted 3 years ago #

    I get a fatal error that points to the line: get_term($request['your_custom_taxonomy'],'your_custom_taxonomy')->name;
    Is there a typo there somewhere? It's hard to tell!

  12. Andrew Nacin
    Lead Developer
    Posted 3 years ago #

    It depends on the fatal error. Most likely, it's a parse error due to PHP4 (you can't do ->name off a function like that in PHP4).

    What is the error?

  13. Cammy_WP
    Member
    Posted 3 years ago #

    I tried the code, but its not working for me.... the filter does appears in the control panel in the custom post_type. but when applying the filter, nothing is displayed...
    I have no idea what I might do wrong...

    add_action('restrict_manage_posts','specials_restrict_manage_posts');
    	function specials_restrict_manage_posts() {
    		global $typenow;
    
    		if ($typenow=='specials'){
    			 $args = array(
    				 'show_option_all' => "Show All Categories",
    				 'taxonomy'        => 'type',
    				 'name'          	=> 'type',
    			);
    
    			wp_dropdown_categories($args);
    		}
    	}
    
    	add_action( 'request', 'my_request' );
    	function my_request($request) {
    
    		if (is_admin() && isset($request['post_type']) && $request['post_type']=='specials') {
    			$request['term'] = get_term($request['type'],'type')->name;
    	}
    	return $request;
    	}
  14. styledev
    Member
    Posted 3 years ago #

    Here is all the code you need to get this custom post type filter to work:

    add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
    function my_restrict_manage_posts() {
    	global $typenow;
    	$taxonomy = $typenow.'_type';
    	if( $typenow != "page" && $typenow != "post" ){
    		$filters = array($taxonomy);
    		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=''>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>";
    		}
    	}
    }

    This line: $taxonomy = $typenow.'_type'; has '_type' at the end of it because my custom taxonomy looks like this:

    register_taxonomy('event_type','event',create_taxonomy_args('event',true));

    So what ever you add on as a suffix (if you do that) you need to change in the code above.

  15. Cammy_WP
    Member
    Posted 3 years ago #

    Marvelous!!!
    Genius!!

    Really, was struggling over for more than a week!
    This is the solution above all others post you can find out there :)

    Cheers!

    Million thanks for the guidance!

Topic Closed

This topic has been closed to new replies.

About this Topic