4

So, I asked this question differently in another thread but it was more of an advice type question so it really didn't render any useful responses. I believe I can word it better and should help some of you decipher what I am trying to accomplish and possibly help someone else once we collaborate on a solution.

I have a custom post type that is using very limited Supports (described here) values because I only need the meta-box values for my custom post type. It doesn't make sense for my type to include a title and content. Code below:

register_post_type( 'athlete',
        array(
            'labels' => array(
                'name' => 'Athletes',
                'singular_name' => 'Athlete',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Athlete',
                'edit' => 'Edit',
                'edit_item' => 'Edit Athlete',
                'new_item' => 'New Athlete',
                'view' => 'View',
                'view_item' => 'View Athlete',
                'search_items' => 'Search Athletes',
                'not_found' => 'No Athletes found',
                'not_found_in_trash' => 'No Athletes found in Trash',
                'parent' => 'Parent Athlete'
            ),

            'public' => true,
            'menu_position' => 15,
            'supports' => array( 'thumbnail' ),
            'taxonomies' => array( '' ),
            'has_archive' => true
        )
    );

As you can see, I only need the thumbnail supports option because the rest of my post type will be meta-boxes (not including in this question). The problem is, when I save an Athlete, I have two problems, I do not know how to display my meta-box values in the Athlete list (as columns in the grid) and I do not know how to set the Title value as it always sets it to "Auto-Generated" which is not preferred. If the Title will be a field that is searchable, I would prefer to set the value to [First Name]+[Last Name]. Can anyone help with these two issues and explain if I am going to run into any problems by using a custom post type as opposed to using custom database tables and a custom UI to manage my object?

5
  • It sounds to me like you are using a custom post type for something that isn't a very good fit for a custom post type. You've really taken most of the 'post' out of the 'type'. I'd seriously consider a custom table.
    – s_ha_dum
    Commented Feb 27, 2013 at 14:30
  • @s_ha_dum I beg to differ. Custom post types are flexible enough where you can have them be whatever you want, with the right plugins and/or code Commented Feb 27, 2013 at 17:22
  • @MannyFleurmond ... and you can drive a screw with a hammer but that doesn't mean you should. CPTs are flexible but they aren't a fit for everything. There is a distinct and unalterable structure to data stored as a custom post type and that structure is not a reasonable structure for some types of data. In this case, the OP seems to want only the meta data and none of the post type, so why use the post type? I have no doubt that you could make it work but why do so if it isn't really a fit?
    – s_ha_dum
    Commented Feb 27, 2013 at 17:42
  • 1
    @s_ha_dum because instead of recreating the UI and data queries from scratch, he can piggy back off of WP's UI and add his/her own spin. I think of CPT's as data objects you can augment via meta data and taxonomies, which is what he's doing. They aren't just posts, at least not anymore. Commented Feb 27, 2013 at 17:46
  • @MannyFleurmond ... but they are posts, or very post-like. Look at the tables. Those are not general purpose tables (I don't know what would be), and the PHP that manipulates the data in those tables is not general purpose. There is a lot of activity that is needed for post-like data but not for other data. And yes, you can piggy-back on the GUI but that GUI also is not a fit for some data. And the CPT queries are not very efficient for some types of data. I get that it might be easier, but not always better. Anyway, this is not the place to argue the point so I won't be commenting again.
    – s_ha_dum
    Commented Feb 27, 2013 at 18:03

1 Answer 1

3

I've dabbled in this as well. For a meta box I recommend the Meta Box plugin (which I regularly contribute code to). A good tutorial on how to use it is here. For custom columns, do a search in WPSE but this should get you started. Saving the post title involves using the save_post filter. When you set up your meta box, remember the id you used for the first and last names and replace then in the code below:

add_filter( 'save_post_athlete', 'wpse88655_set_title', 10, 3 );
function wpse88655_set_title ( $post_id, $post, $update ){
    //This temporarily removes filter to prevent infinite loops
    remove_filter( 'save_post_athlete', __FUNCTION__ );

    //get first and last name meta
    $first = get_metadata( 'athelete_first_name', $post_id ); //meta for first name
    $last = get_metadata( 'athelete_last_name', $post_id );   //meta for last name

    $title = $first . ' ' . $last;

    //update title
    wp_update_post( array( 'ID'=>$post_id, 'post_title'=>$title ) );

    //redo filter
    add_filter( 'save_post_athlete', __FUNCTION__, 10, 3 );
}
1
  • Thank you so much for the answwer, and I marked it as such. It does answer the actual question in the title, however, I believe the discussion between you and s_ha_dum was more valuable to me personally because it explained an argument I wanted to have a few days ago on whether or not my end-goal is a good fit for a CPT. Your solution will get me where I want to go if I decide on CPT, but if not, I'll just have to code everything I need (which will take me longer especially since I'm a .Net developer :-/ ).
    – clockwiseq
    Commented Feb 27, 2013 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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