Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've basically done a full plugin and now just "prettifying" it sort-of. One thing I'm doing is re-doing the columns for listing the "posts" I made. This part is fine, and all columns are replaced as needed, however, there's one column that puzzles me.. the title field. :/

    add_action( 'manage_posts_custom_column', array( &$this, '_wp_filter_visitor_column_view' ) );
    add_filter( 'manage_edit-visitor_columns', array( &$this, '_wp_filter_visitor_columns' ) );

    public function _wp_filter_visitor_column_view( $column )
    {
        global $post;
        if ( $column == "title" )
        {
            $name = get_post_meta( $post->ID, 'v_f_name', true );
            $name .= ' ' . get_post_meta( $post->ID, 'v_l_name', true );
            echo $name;
        }
        elseif ( $column == "type" )
        {
            echo "Not available right now...";
        }
        elseif ( $column == "loggedin" OR $column == "workstation" )
        {
            $workstation = get_post_meta( $post->ID, 'v_workstation', true );
            if ( $column == "workstation" ) echo $workstation;
            elseif ( $column == "loggedin" ) echo ( !empty($workstation) OR !isset( $workstation ) ) ? 'No' : 'Yes';
        }
        elseif ( $column == "id" )
        {
            echo get_post_meta( $post->ID, 'v_id', true );
        }
    }

    public function _wp_filter_visitor_columns( $columns )
    {
        $columns = array(
                'cb' => '<input type="checkbox" />',
                'title' => 'Name',
                'type' => 'Type',
                'loggedin' => 'Logged In?',
                'id' => 'Visitor ID',
            );

        return $columns;
    }

The column header for "title" => "Name" appears properly, but the column data displays the default title of "Auto Draft" because I'm not using the post's title field. Do I need to the post's title field for custom post types or will auto-generated titles suffice? I have no need for the title field and would rather use the postmeta table for storing my data.

-Zack

P.S. I was following this tutorial for creating the columns.

share|improve this question
    
Can you clarify what you are asking? Are you simply asking it you need the title field, or are you asking how to get "Auto Draft" to not be displayed in the column list, or something else? –  MikeSchinkel Jan 13 '11 at 23:28

2 Answers 2

Hi @Zack:

I'm going out on a limb here since your question wasn't clear. It sounds like you want to be able to display the first and last name of the visitor in the vistor listing instead of "Auto Draft?" If so this code should work for you:

add_action( 'the_title', 'make_vistor_title', 10, 2 );
function make_vistor_title($title,$post_id) {
  global $pagenow,$typenow;
  if ($pagenow=='edit.php' && $typenow=='visitor') {
    return the_visitor_title($post->ID);
  }
  return $title;
}    
function the_visitor_title($post_id) {
  return get_post_meta( $post_id, 'v_f_name', true ) . ' ' . 
         get_post_meta( $post_id, 'v_l_name', true );
}

If you want the title to work no matter where you are use this version of make_visitor_title() instead:

function make_vistor_title($title,$post_id) {
  $post = get_post($post_id);
  if ($post->post_type=='visitor') {
    $title = the_visitor_title($post->ID);
  }
  return $title;
}    

Of course another (better?) option would be to use capture the names on POST and create a title based on the values in your two (2) custom meta fields 'v_f_name' and 'v_l_name' using code like the following (which is far more complex that I would like it to be but that's what it took me to get it working - maybe someone else could suggest a simpler way?):

add_action( 'admin_init', 'visitor_post_admin_init', 10, 2 );
function visitor_post_admin_init() {
  global $pagenow;
  if ($pagenow=='post.php' && isset($_POST['action']) && $_POST['action']=='editpost') {
    if (empty($_POST['post_title'])) {
      $_POST['post_title'] = get_visitor_title_from_POST($_POST);
    }
  }
}
add_action( 'wp_insert_post_data', 'insert_visitor_data', 10, 2 );
function insert_visitor_data($data,$postarr) {
  if ($data['post_type']=='actor' && !in_array($data['post_status'],array('draft','auto-draft'))) {
    if (isset($postarr['meta'])) {
      extract($postarr);
      $post_title = get_visitor_title_from_POST($postarr);
      if (!empty($post_title))
        $data['post_title'] = trim($post_title);
      $post_name = sanitize_title_with_dashes($post_title);
      $data['post_name'] = wp_unique_post_slug($post_name, $ID, $post_status, $post_type, $post_parent);
    }
  }
  return $data;
}
function get_visitor_title_from_POST($POST) {
  $meta = array();
  foreach($POST['meta'] as $key_value)
    $meta[$key_value['key']] = $key_value['value'];
  if (isset($POST['metavalue'])) {
    if (isset($POST['metakeyselect']) && $POST['metakeyselect']!='#NONE#') {
      $meta[$POST['metakeyselect']] = $POST['metavalue'];
    } else {
      $meta[$POST['metakeyinput']] = $POST['metavalue'];
    }
  }
  $title = (isset($meta['v_f_name']) ? "{$meta['v_f_name']} " : '');
  if (isset($meta['v_l_name']))
    $title .= $meta['v_l_name'];
  return trim($title);
}
share|improve this answer
    
Yea sorry, I didn't check stackexchange again last night after I left work so I didn't see your initial comment. Also, I think I'll go for the second option - which I thought of but wasn't sure whether it was "proper" to do that. –  Zack Jan 14 '11 at 17:21
    
Quick extension question about your answer. I'm currently using the save_post hook. I can throw this into that hook's body, right? Or do I need wp_insert_post_data? –  Zack Jan 14 '11 at 17:34
up vote 0 down vote accepted

I did a slight hack for this to work. I added a hidden field named "post_title" as if I'm enabling Title support, though without actually having the ugly title bar positioned.

<div id="titlewrap"> 
    <input type="hidden" name="post_title" size="30" tabindex="1" value="" id="title" autocomplete="off" />
</div>

<script type="text/javascript">
    ( function ($) {
        $( '#v_l_name' ).blur( function () {
            $( '#title' ).attr( 'value', $( '#v_f_name' ).val() + ' ' + $( '#v_l_name' ).val() );
        });
    })(jQuery);
</script>

All-in-all, It works. The title field is populated properly.

Thanks Mike though! ;)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.