adding admin filter for funds by fund group
authorAnthony Talarico <talarico@gaslightmedia.com>
Fri, 26 May 2017 15:37:09 +0000 (11:37 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Fri, 26 May 2017 15:37:09 +0000 (11:37 -0400)
adding the dropdown menu for fund groups to filter the fund types in the admin menu

lib/funds.php

index 75cc07f..8297f13 100644 (file)
@@ -40,7 +40,7 @@ function custom_post_type() {
        );
        $args = array(
                'label'                 => __( 'Fund', 'text_domain' ),
-               'description'           => __( 'Funds', 'text_domain' ),
+               'description'           => __( 'Custom', 'Funds' ),
                'labels'                => $labels,
                'supports'              => array( 'title', 'page-attributes' ),
                'hierarchical'          => false,
@@ -76,7 +76,7 @@ function fund_taxonomies()
         'update_item'       => __( 'Update ' . CATEGORY_SINGLE),
         'add_new_item'      => __( 'Add New ' . CATEGORY_SINGLE),
         'new_item_name'     => __( 'New ' . CATEGORY_SINGLE . ' Name' ),
-        'menu_name'         => __( 'Product ' . CATEGORY_SINGLE ),
+        'menu_name'         => __(  CATEGORY_SINGLE ),
     );
 
     $capabilities = array(
@@ -137,6 +137,95 @@ function funds_shortcode($atts)
 add_action( 'init', 'fund_taxonomies' );
 add_action( 'init', 'custom_post_type' );
 add_shortcode( 'list-funds', 'funds_shortcode' );
+/**
+ * michsci_admin_custom_columns
+ *
+ * This will generate the column data for eaoch record.
+ *
+ * @param mixed $column
+ * @access public
+ * @return void
+ */
+function funds_admin_custom_columns($column)
+{
+    global $post;
+  
+    switch ($column) {
+    case 'fund_col_cat':
+        $fund_cats      = get_the_terms( $post->ID, 'fund_group' );
+        $fund_cats_html = array();
+        if ( $fund_cats ) {
+            foreach ( $fund_cats as $fund_cat ) {
+                array_push( $fund_cats_html, $fund_cat->name );
+            }
+            echo implode( $fund_cats_html, ', ' );
+        } else {
+             _e( 'None', 'themeforce' );
+        }
+        break;
+    }
+}
+function funds_admin_edit_columns($columns)
+{       
+    $columns = array(
+        'cb'                  => '<input type="checkbox">',
+        'title'               => FUND_SINGLE . ' Title',
+        'fund_col_cat'        => CATEGORY_SINGLE
+    );
+    return $columns;
+}
+function fund_set_admin_columns()
+{
+    add_filter( 'manage_edit-fund_columns', 'funds_admin_edit_columns' );
+    add_action( 'manage_fund_posts_custom_column', 'funds_admin_custom_columns' );
+}
+
+add_action( 'admin_init', 'fund_set_admin_columns' );
+function fund_taxonomy_filter_restrict_manage_posts()
+{
+    global $typenow;
+
+    $post_types = get_post_types( array( '_builtin' => false ) );
+
+    if ( in_array( $typenow, $post_types ) ) {
+        $filters = get_object_taxonomies( $typenow );
+
+        foreach ( $filters as $tax_slug ) {
+           
+            $tax_obj = get_taxonomy( $tax_slug );
+            wp_dropdown_categories( array(
+                'show_option_all' => __( 'Show All ' . $tax_obj->label ),
+                'taxonomy'        => $tax_slug,
+                'name'            => $tax_obj->name,
+                'orderby'         => 'name',
+                'selected'        => ( isset( $_GET[$tax_slug] ) ),
+                'hierarchical'    => $tax_obj->hierarchical,
+                'show_count'      => false,
+                'hide_empty'      => true
+            ) );
+        }
+    }
+}
+
+add_action( 'restrict_manage_posts', 'fund_taxonomy_filter_restrict_manage_posts' );
+function fund_taxonomy_filter_post_type_request( $query )
+{
+    global $pagenow, $typenow;
+
+    if ( 'edit.php' == $pagenow ) {
+        $filters = get_object_taxonomies( $typenow );
+        foreach ( $filters as $tax_slug ) {
+            $var = &$query->query_vars[$tax_slug];
+
+            if ( isset( $var ) ) {
+                $term = get_term_by( 'id', $var, $tax_slug );
+
+                $var = $term->slug;
+            }
+        }
+    }
+}
+add_filter( 'parse_query', 'fund_taxonomy_filter_post_type_request' );
 
 /**
  * Save Meta Data