WordPress.org

Ready to get started?Download WordPress

Forums

Custom Column for Title using Custom Fields (4 posts)

  1. thecorkboard
    Member
    Posted 3 years ago #

    Hello-

    I've built a staff directory but I've removed the title metabox because I use a "first name" and "last name" custom field to supply this. The problem is in my custom columns.

    I can remove the title column and replace it with a combined one made out of my fields, but the nice hover over that brings up "Edit | Quick Edit | Trash | View" is gone, I'm assuming, because the title column isn't in existence.

    A side problem is that the menu editor shows these custom post types as "Auto Draft" for the title (since I never input a "real" title).

    Thanks for your input.

    ~Kyle~

    Here are the custom columns:

    /*Custom Columns*/
    add_filter("manage_edit-staff-directory_columns", "staff_directory_columns"); 		//anything can go after "manage_edit-"
    add_action("manage_posts_custom_column",  "staff_directory_custom_columns");		//"manage_posts_custom_column" must stay as is
    
    function staff_directory_columns($columns){
    		$columns = array(
    			"cb" => "<input type=\"checkbox\" />",
    			"staff_directory_picture" => "Picture",
    			"staff_directory_name" => "Name (<span style='font-weight: normal'>Last, First</span>)",
    			"staff_directory_title" => "Title",
    			"staff_directory_department" => "Department",
    			"staff_directory_email" => "E-mail",
    			"staff_directory_phone_number" => "Phone Number",
    		);
    		return $columns;
    }
    
    function staff_directory_custom_columns($column){
    		global $post;
    		switch ($column)
    		{
    			case "staff_directory_picture":
    				echo "<img width='75px' height='75px' src='";
    				$key="bio_picture"; echo get_post_meta($post->ID, $key, true);
    				echo "'/>";
    				break;
    			case "staff_directory_name":
    				echo "<a href='../wp-admin/post.php?post="; echo the_ID(); echo "&action=edit'>";
    				$key="name_last_name"; echo get_post_meta($post->ID, $key, true); echo ', '; $key="name_first_name"; echo get_post_meta($post->ID, $key, true);
    				echo "</a>";
    				break;
    			case "staff_directory_department":
    				echo get_the_term_list($post->ID, 'department', '', ', ','');
    				break;
    			case "staff_directory_title":
    				$key="name_title"; echo get_post_meta($post->ID, $key, true);
    				break;
    			case "staff_directory_email":
    				$key="contact_email"; echo get_post_meta($post->ID, $key, true);
    				break;
    			case "staff_directory_phone_number":
    				$key="contact_phone_number"; echo get_post_meta($post->ID, $key, true);
    				break;
    		}
    }
  2. ozzierod
    Member
    Posted 3 years ago #

    I have the same problem. Ever find out how to fix it?

  3. ozzierod
    Member
    Posted 3 years ago #

    Probably a better way but here's my hack.

    Edit wp-admin/includes/class-wp-posts-list-table.php - single_row function
    There's a switch statement in that class that lists 'title' as a case.

    Copy the 'title'. In my situation, my custom page type is not hierarchical so i removed that if statement. Replace the $title's with your custom field and voila!

    Here's my modified case

    case 'location_name':
    					$attributes = 'class="post-title page-title column-title"' . $style;
    ?>
    			<td <?php echo $attributes ?>><strong><?php if ( $can_edit_post && $post->post_status != 'trash' ) { ?><a class="row-title" href="<?php echo $edit_link; ?>" title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), get_post_meta($post->ID, "location_name", true) ) ); ?>"><?php echo get_post_meta($post->ID, "location_name", true) ?></a><?php } else { echo $title; }; _post_states( $post ); ?></strong>
    <?php
    					if ( 'excerpt' == $mode ) {
    						the_excerpt();
    					}
    
    				$actions = array();
    				if ( $can_edit_post && 'trash' != $post->post_status ) {
    					$actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Edit this item' ) ) . '">' . __( 'Edit' ) . '</a>';
    					$actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="' . esc_attr( __( 'Edit this item inline' ) ) . '">' . __( 'Quick&nbsp;Edit' ) . '</a>';
    				}
    				if ( current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) {
    					if ( 'trash' == $post->post_status )
    						$actions['untrash'] = "<a title='" . esc_attr( __( 'Restore this item from the Trash' ) ) . "' href='" . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', $post->ID ) ), 'untrash-' . $post->post_type . '_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
    					elseif ( EMPTY_TRASH_DAYS )
    						$actions['trash'] = "<a class='submitdelete' title='" . esc_attr( __( 'Move this item to the Trash' ) ) . "' href='" . get_delete_post_link( $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
    					if ( 'trash' == $post->post_status || !EMPTY_TRASH_DAYS )
    						$actions['delete'] = "<a class='submitdelete' title='" . esc_attr( __( 'Delete this item permanently' ) ) . "' href='" . get_delete_post_link( $post->ID, '', true ) . "'>" . __( 'Delete Permanently' ) . "</a>";
    				}
    				if ( in_array( $post->post_status, array( 'pending', 'draft' ) ) ) {
    					if ( $can_edit_post )
    						$actions['view'] = '<a href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) . '" title="' . esc_attr( sprintf( __( 'Preview “%s”' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
    				} elseif ( 'trash' != $post->post_status ) {
    					$actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
    				}
    
    				$actions = apply_filters( is_post_type_hierarchical( $post->post_type ) ? 'page_row_actions' : 'post_row_actions', $actions, $post );
    				echo $this->row_actions( $actions );
    
    				get_inline_data( $post );
    				echo '</td>';
    			break;
  4. JTWilcox
    Member
    Posted 3 years ago #

    Has anyone else found a different way to do this? ozzierod's way would probably work but I want to be able to change this from within my theme so that updates won't ruin the hack. I have the exact same issue as the OP.

Topic Closed

This topic has been closed to new replies.

About this Topic