Support

Account

Home Forums Feature Requests Admin CPT List View Column Controls

Solved

Admin CPT List View Column Controls

  • I have a field group associated to a CPT and it makes the data entry side ridiculously simple. It would be nice if there were a selection option in each field setting to specify whether it should also:

    • Be displayed on the list view for the associated post type?
    • Be sortable if displayed on the list view?
    • Be filterable if displayed on the list view?

    For example….The following snippets of code (used within a class-based plugin definition) are required to have 2 meta data columns -be displayed on list view for a CPT (job) and sortable. Only the job_city meta data is filterable.

    Filters and Actions

    
    add_filter("manage_edit-job_columns", array(&$this, "edit_columns"));
    add_action("manage_posts_custom_column", array(&$this, "custom_columns"));
    add_filter("manage_edit-job_sortable_columns", array(&$this, "sorting_columns" ));
    add_filter("request", array(&$this, "job_reqnumber_column_orderby" ));
    add_filter("request", array(&$this, "job_city_column_orderby" ));
    add_action( "restrict_manage_posts", array(&$this, "job_filter_city" ));
    add_action( "parse_query", array(&$this, "job_filter_query_city" ));
    

    The functions for them

    
    function edit_columns($columns)
    	{
    		$columns = array(
    			"cb" => "<input type=\"checkbox\" />",
    			"title" => "Job",
    			"job_reqnumber" => "Requisition #",
    			"job_city" => "City",
    			"icl_translations" => "<img width=\"18\" height=\"12\" title=\"Francais\" alt=\"Francais\" src=\"/wp-content/plugins/sitepress-multilingual-cms/res/flags/fr.png\">",
    			"date"=>"Date"
    		);
    		
    		return $columns;
    	}
    	
    	function custom_columns($column)
    	{
    		global $post;
    		switch ($column)
    		{
    			case "job_reqnumber":
    				echo get_field('job_reqnumber');
    				break;
    			case "job_city":
    				echo get_field('job_city');
    				break;
    			
    		}
    	}
    	
    	
    	
    	function sorting_columns( $columns ) {
    		$columns['job_reqnumber'] = 'job_reqnumber';
    		$columns['job_city'] = 'job_city';
    		return $columns;
    	}
    
    	function job_reqnumber_column_orderby( $vars ) {
        	if ( isset( $vars['orderby'] ) && 'job_reqnumber' == $vars['orderby'] ) {
    	        $vars = array_merge( $vars, array(
                	'meta_key' => 'job_reqnumber',
                	'orderby' => 'meta_value'
            	) );
        	}
    
        	return $vars;
    	}
    	
    	function job_city_column_orderby( $vars ) {
        	if ( isset( $vars['orderby'] ) && 'job_city' == $vars['orderby'] ) {
    	        $vars = array_merge( $vars, array(
                	'meta_key' => 'job_city',
                	'orderby' => 'meta_value'
            	) );
        	}
    
        	return $vars;
    	}
    
    	function job_filter_city( $query ) {
        	
    		global $typenow;
        	global $wp_query;
        		if ($typenow=='job') {
        			
    				echo '<select id="city" name="city">';
    				echo '<option value="">Show all cities</option>';
    				
        			$arr = array("Ottawa", "Toronto", "Mississauga");
    				foreach ($arr as &$value) {
        				if( !empty( $_GET['city']) && $_GET['city'] == $value ) {
        					echo '<option selected="selected">' . $value . '</option>';		
    					} else {
    						echo '<option>' . $value . '</option>';
    					}
    				}
    
            		 echo '</select>';
        	}	
    		
    	}
    	
        function job_filter_query_city( $query ) {
    		if( is_admin() AND $query->query['post_type'] == 'job' ) {
    			$qv = &$query->query_vars;
    			$qv['meta_query'] = array();
    	
    	
    			if( !empty( $_GET['city'] ) ) {
    				$qv['meta_query'][] = array(
    					'field' => 'job_city',
    					'value' => $_GET['city'],
    					'compare' => '=',
    					'type' => 'CHAR'
    				);
    			}
    	
    			if( !empty( $_GET['orderby'] ) AND $_GET['orderby'] == 'city' ) {
    				$qv['orderby'] = 'meta_value';
    				$qv['meta_key'] = 'job_city';
    				$qv['order'] = strtoupper( $_GET['order'] );
    			}
    		}
    	}
    
  • Hi @Idealien

    Please see the compatible plugins page below to find a link to Admin Custom Columns plugin:

    http://www.advancedcustomfields.com/resources/getting-started/compatible-plugins/

    This plugin will give you a simple interface to customize columns.

    Thanks
    E

  • Beautiful UI.

    Unfortunately whatever is plaguing my dev setup for the get_field functions – http://support.advancedcustomfields.com/forums/topic/cpt-loop-not-returning-get_field-but-does-get_post_meta/ – is also prohibiding the CodePress Admin Columns plugin from identifying my custom fields for selection.

    But I will find a solution for that and then can see these both working very well together.

  • This reply has been marked as private.
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Admin CPT List View Column Controls’ is closed to new replies.