WordPress.org

Ready to get started?Download WordPress

Forums

Is it possible to order custom columns by a custom taxonomy? (1 post)

  1. IridescentShadow
    Member
    Posted 2 years ago #

    Does anyone know a way to order custom columns in edit mode for a custom post type by a custom taxonomy?

    I've written a plugin that adds a custom post type called product and a custom taxonomy called brand. I added a column to the edit mode using the manage_edit-product_columns filter hook that shows the brand, and added value support through the manage_product_posts_custom_column action hook.

    <?php if (is_admin()) {
    	add_action("admin_init","products_admin_init");
    }
    function products_admin_init() {
    	//Add custom columns to edit view
    	add_filter("manage_edit-product_columns","add_product_columns");
    	add_action("manage_product_posts_custom_column","manage_product_columns",10,2);
    }
    //Add custom columns to edit view
    function add_product_columns($product_columns) {
    	$new_columns['cb'] = "<input type=\"checkbox\" />";
    
    	$new_columns['title'] = "Product Name";
    	$new_columns['image'] = "Image";
    	$new_columns['brand'] = "Brand";
    
    	return $new_columns;
    }
    function manage_product_columns($column_name,$id) {
    	global $wpdb;
    
    	switch ($column_name) {
    		case "image":
    			$image = wp_get_attachment_image_src(get_post_thumbnail_id($id),"single-post-thumbnail");
    			echo "<img src=\"".$image[0]."\" style=\"max-width:50px;max-height:50px;\" />";
    		break;
    		case "brand":
    			$brands = get_the_term_list($id,"brand","","|");
    			echo $brands;
    		break;
    	}
    }

    That works well at showing the value of my brand taxonomy but the column wasn't sortable. I found example code on how to make those sortable and implemented it, but it doesn't work. Here's the sorting code:

    function products_admin_init() {
    	//Add custom columns to edit view
    	add_filter("manage_edit-product_columns","add_product_columns");
    	add_action("manage_product_posts_custom_column","manage_product_columns",10,2);
    	add_filter("manage_edit-product_sortable_columns","make_product_columns_sortable");
    	add_action("load-edit.php","modify_product_edit_behavior");
    }
    function make_product_columns_sortable($sortable_columns) {
    	$sortable_columns['brand'] = "brand";
    
    	return $sortable_columns;
    }
    function modify_product_edit_behavior() {
    	add_filter("request","product_columns_sort_handler");
    }
    function product_columns_sort_handler($vars) {
    	//Check if we're viewing the 'product' post type
    	if (isset($vars['post_type']) && $vars['post_type'] == "product") {
    		//Add handlers for custom 'orderby' values
    		switch ($vars['orderby']) {
    			case "brand":
    				//Merge the query vars with our custom variables
    				$vars = array_merge($vars,array(
    					'meta_key' => 'brand',
    					'orderby' => 'meta_value_num'
    				));
    			break;
    		}
    	}
    
    	return $vars;
    }

    I think the issue lies in product_columns_sort_handler() where I specify the meta_key. That only seems to work if I try to sort by a custom field that was added using update_post_meta(). Is there a way to make the query sort by the values of my custom taxonomy called brand?

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by IridescentShadow
  • This topic is not resolved
  • WordPress version: 3.3.2