3

I am trying to create a member directory and using a custom post type members. I am familiar on how to use the ACF plugin but is there any methods on how to associate a custom field for a specific custom post type to have a simple input box like phone number and address?

  • Custom fields are already a build in feature. You just need to add support for it when registering your custom post type ;-) – Pieter Goosen Dec 13 '15 at 6:18
  • Thanks Pieter but is there a way to add a code so we dont have to select the custom field for each adding of a new entry?I'll try Carl's code first to see if that works – Mac organo Dec 14 '15 at 13:49
4

If you don't want to use a plugin, you can try implementing a meta box to be associated with that custom post type, This is the code that I use if I want to add a simple input field for a certain post type, in the example below, I assume that the member_post_type is your declared post type:

function member_add_meta_box() {
//this will add the metabox for the member post type
$screens = array( 'member_post_type' );

foreach ( $screens as $screen ) {

    add_meta_box(
        'member_sectionid',
        __( 'Member Details', 'member_textdomain' ),
        'member_meta_box_callback',
        $screen
    );
 }
}
add_action( 'add_meta_boxes', 'member_add_meta_box' );

/**
 * Prints the box content.
 *
 * @param WP_Post $post The object for the current post/page.
 */
function member_meta_box_callback( $post ) {

// Add a nonce field so we can check for it later.
wp_nonce_field( 'member_save_meta_box_data', 'member_meta_box_nonce' );

/*
 * Use get_post_meta() to retrieve an existing value
 * from the database and use the value for the form.
 */
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );

echo '<label for="member_new_field">';
_e( 'Phone Number', 'member_textdomain' );
echo '</label> ';
echo '<input type="text" id="member_new_field" name="member_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
 function member_save_meta_box_data( $post_id ) {

 if ( ! isset( $_POST['member_meta_box_nonce'] ) ) {
    return;
 }

 if ( ! wp_verify_nonce( $_POST['member_meta_box_nonce'], 'member_save_meta_box_data' ) ) {
    return;
 }

 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
 }

 // Check the user's permissions.
 if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) ) {
        return;
    }

 } else {

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
 }

 if ( ! isset( $_POST['member_new_field'] ) ) {
    return;
 }

 $my_data = sanitize_text_field( $_POST['member_new_field'] );

 update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'member_save_meta_box_data' );

Additional Resources for the WordPress Metaboxes:

please see documentation here

  • I tried adding your code in my theme's function.php and I have new metabox in my custom post type. Would you happen to know if I can also control s metabox order if it would be displayed first or at the last part? – Mac organo Dec 14 '15 at 13:56
  • @Macorgano Yes, you can. The fifth parameter to add_meta_box indicates where you want it to be while the sixth indicates the priority. You really should check out the documentation. – Ezra Obiwale Jun 5 at 6:07
2

As I already stated in comments,

Custom fields are already a build in feature. You just need to add support for it when registering your custom post type ;-)

You have two options here:

  • When you register your custom post type, simply add custom-fields to the supports parameter

    Example:

    add_action( 'init', 'codex_custom_init' );
    function codex_custom_init() {
        $args = [
          'public' => true,
          'label'  => 'Books',
          'supports' => ['title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields']
        ];
        register_post_type( 'book', $args );
    }
    
  • You can simply use add_post_type_support() to add custom fields to your custom post type

    Example:

    add_action( 'init', 'wpcodex_add_custom_fileds_support_for_cpt', 11 );
    function wpcodex_add_custom_fileds_support_for_cpt() {
        add_post_type_support( 'cpt', 'custom-fields' ); // Change cpt to your post type
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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