WordPress.org

Ready to get started?Download WordPress

Forums

manage_posts_custom_column not working (13 posts)

  1. nicdvs
    Member
    Posted 3 years ago #

    Hi,

    I created a custom post type of 'Portfolio' that work well.

    On the 'Portfolio' page (where you see all post uner Portfolio..) I changed the columns with this code: (it works well)

    add_filter('manage_edit-portfolio_columns', 'portfolio_edit_columns');
    function portfolio_edit_columns($columns){
    	$columns = array(
    		'cb' => '<input type="checkbox" />',
    		'title' => 'Project Title',
    		'description' => 'Description',
    		'skills' => 'Skills',
    		'date_completed' => 'Date Completed',
    	);
    	return $columns;
    }

    Now I'm trying to populate those columns but it doesn't work.. I looked at the Codex and am using the same code as them but it still doesn't work..

    add_action('manage_posts_custom_column', 'portfolio_custom_columns');
    function portfolio_custom_columns($column){
    	global $post;
    	switch ($column){
    		case 'description':
    			the_excerpt();
    			break;
    		case 'skills':
    			$terms = get_the_term_list( $post->ID , 'skills' , '' , ', ' , '' );
    			if( is_string( $terms ) ){
    				echo $terms;
    			}else{
    				echo 'Unable to get skill(s)';
    			}
    			break;
    		case 'date_completed':
    			echo 'date';
    			break;
    	}
    }

    In the admin section I can see the new columns but they are empty, even the date completed (which only echo 'date') is empty.. Looks like the function never gets called..

    Any help is appreciated..

    Thanks,

    Nicolas

  2. Richard Tape
    Member
    Posted 3 years ago #

    Nicolas, I think with the latest stable release of WP, if your post type has hierarchical => true, then the custom columns don't work. Try setting it to false and see if they work.

    FWIW, I think this is 'fixed' in the betas of 3.1.

    You can also add column sorting via AJAX - wont work in 3.0.* but will work in 3.1 like so:

    // Add the sorting SQL for the themes
    	function my_themes_price_column_orderby($orderby, $wp_query)
    	{
    
    		global $wpdb,$post;
    
    		$wp_query->query = wp_parse_args($wp_query->query);
    
    		if ( 'price' == @$wp_query->query['orderby'] )
    		{
    			$orderby = "(SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $wpdb->posts.ID AND meta_key = '_price_of_theme') " . $wp_query->get('order');
    
    		}
    
    		if ( 'DemoURL' == @$wp_query->query['orderby'] )
    		{
    			$orderby = "(SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $wpdb->posts.ID AND meta_key = '_demo_location_of_theme') " . $wp_query->get('order');
    
    		}
    
    		if ( 'ID' == @$wp_query->query['orderby'] )
    		{
    			$orderby = "(SELECT distinct post_id FROM $wpdb->postmeta WHERE post_id = $wpdb->posts.ID) " . $wp_query->get('order');
    
    		}
    
    		return $orderby;
    
    	}
    
    	add_filter('posts_orderby', 'my_themes_price_column_orderby', 10, 2);
  3. nicdvs
    Member
    Posted 3 years ago #

    Hey there,

    thanks for the response, you are right, my custom post type has hierarchical set to true and if I set it to false, the columns gets populated!

    I since then updated my WP version to the latest trunk (3.1.something) and everything is working fine.. I guess this was still a new feature in 3.0...

    And also, thanks for the AJAX column sorting thing, I had completely forgotten about that and thought it was built in.. Will absolutely implement this!

    Thanks again,

    Nicolas

  4. syllogism
    Member
    Posted 3 years ago #

    Thought I would post this query here as it is somewhat related.

    I have tried sorting columns in 3.1-RC2 per iamfriendly's suggestion but can not make it work. Custom columns are created in very much the same way as nicdvs. Have also tried registering the columns using manage_edit-post_sortable_columns, but that does not help either.

    Believe it may be an issue with the use of an array when creating the custom columns.

    Thoughts?

  5. Rutwick Gangurde
    Member
    Posted 3 years ago #

    Hi,
    I had the same problem as nicdvs, so I set the 'hieracrhial' => false to display my columns. I have WP 3.1, and the problem is not yet fixed.

    What if we want hierarchies for custom posts?

  6. samanime
    Member
    Posted 3 years ago #

    Just to update this, if you want to use hierarchical post types, use manage_page_custom_columns instead.

  7. alexleonard
    Member
    Posted 3 years ago #

    This was totally baffling me. Thanks for the pointer! I was going around in circles for quite a while. Huzzah for the forums :)

  8. squarecandy
    Member
    Posted 3 years ago #

    Uggg! Thanks for the tip iamfriendly... Was messing with that for a long time.

    Do we know if this is something that should be reported as a bug, or if it works this way by design for some odd reason?

    I'm still having the issue in 3.1.2

  9. danblaker
    Member
    Posted 3 years ago #

    In 3.1.x, the syntax is different for managing custom columns for custom post types. You would use the following code to add a column for a custom taxonomy named "foo_skill" on the edit page for a custom post type named "foo_portfolio" (always namespace your custom post types and taxonomies using a unique prefix!):

    add_filter('manage_foo_portfolio_posts_columns', 'manage_foo_portfolio_columns');
    add_action('manage_foo_portfolio_posts_custom_column', 'print_foo_portfolio_column'), 10, 2);
    
    function manage_foo_portfolio_columns($existing_columns) {
      $existing_columns['foo_skill'] = 'Skills';
      return $existing_columns;
    }
    
    function print_foo_portfolio_column($column_name, $post_id) {
      if( $column_name == 'foo_skill' )
      {
        global $typenow; // global WP post type variable
        $terms = get_the_terms($post_id, 'foo_skill');
    		if ( !empty( $terms ) )
    		{
    			$out = array();
    			foreach ( $terms as $term )
    				$out[] = '<a href="edit.php?post_type=' . $typenow . '&foo_skill=' . $term->slug . '">' .
    			    esc_html(sanitize_term_field('name', $term->name, $term->term_id, 'foo_skill', 'display')) .
    			  '</a>';
    			echo join( ', ', $out );
    		}
    		else
    		{
    			echo 'No Skills.';  //No Taxonomy term defined
    		}
      }
    }
  10. kreviii
    Member
    Posted 3 years ago #

    @danblankerk
    I think there is an extra ')' in your code there... on the add action function...

    otherwise worked for me ! , how can you use this code to make a filter button at the top ? and where is all this documented in the codex ? I can't seem to find clear examples about all this stuff.

  11. helgatheviking
    Member
    Posted 3 years ago #

    +1 for danblankerk! saved me a lot of hair-pulling when i switched to hierarchical post types and all my columns disappeared

  12. drrobotnik
    Member
    Posted 2 years ago #

    if your post type has hierarchical => true, then the custom columns don't work. Try setting it to false and see if they work.

    Just felt like adding to this since I ran into an instance where I needed my custom post type to be hierarchical to use wp_list_pages() with custom post types on the front end.

    adding a conditional around is_admin() to check if you're on the front end or the backend will solve this quirky bug.

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    	if(is_admin()){$hier = false;}else{$hier = true;}
    	register_post_type( 'projects',
    		array(
    			'labels' => array(
    				'name' => __( 'Projects' ),
    				'singular_name' => __( 'Project' ),
    				'add_new_item' => __( 'Add New Project' ),
    				'edit_item' => __( 'Edit Project' )
    			),
    			'public' => true,
    			'has_archive' => true,
    			'hierarchical' => $hier,
    			'rewrite' => array('slug' => 'project')
    		)
    	);
    }
  13. inderpreet99
    Member
    Posted 2 years ago #

    For hierarchical post types, use 'manage_pages_custom_column' action hook.

Topic Closed

This topic has been closed to new replies.

About this Topic