简体   繁体   中英

Add custom taxonomy metadata field to WordPress

I'm trying to add a custom metadata field to my custom taxonomy term for a custom post type in WordPress. Basically, I want to add a Subtitle field to my Categories for my Post type.

I was able to add the custom field to my edit form by hooking into the edit_tag_form_fields` action.

function custom_edit_tag_form_fields( $tag ) {
    $meta_type = 'myCustomPostType_myCustomTaxonomy';
    $object_id = $tag->term_id;
    $meta_key = 'subtitle';
    $single = true;
    $value = get_metadata($meta_type, $object_id, $meta_key, $single);
?>
<tr class="form-field term-description-wrap">
    <th scope="row">
        <label for="subtitle">Subtitle</label>
    </th>
    <td>
        <input name="subtitle" id="subtitle" type="text" value="<?php echo $value; ?>" size="40" />
    </td>
</tr>   
<?php
}

In order to save the metadata, I added a hook to the edited_terms action for collecting and saving the inputted data.

function custom_edited_terms( $term_id ) {
    $meta_type = 'myCustomPostType_myCustomTaxonomy';
    $object_id = $term_id;  
    $meta_key = 'subtitle';
    $prev_value = null;

    if( isset($_POST[$meta_key]) ) {
        $meta_value = esc_attr( $_POST[$meta_key] );
        update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value);
    }
}

I'm not exactly sure why it isn't being stored into the database. If I echo $value , I get an empty string. I would expect it to be the value that I type into the input field.

have you read about the term meta data introduced with Wordpress 4.4 ?

function taxonomy_client_extra_field_url_display( $client ) {

    $client_url = get_term_meta( $client->term_id, 'client-url', true );

    ?>
    <tr class="form-field term-description-wrap">
        <th scope="row">
            <label for="client_url"><?php _e( 'Client URL', 'wp-job-manager' );?></label>
        </th>
        <td>
            <input name="term_meta[client-url]" id="client_url" type="text" value="<?php echo $client_url; ?>" size="40" />
        </td>
    </tr>   
    <?php
}

function taxonomy_client_extra_fields_save( $term_id ) {

    if ( !isset( $_POST['term_meta'] ) ) return;

    foreach ( $_POST['term_meta'] as $slug => $value){

        switch($slug){
            default:
                $value = sanitize_title( $value );
            break;
        }

        update_term_meta( $term_id, $slug, $value );
    }

}

add_action( "job_listing_client_add_form_fields", 'taxonomy_client_extra_field_url_display' );
add_action( "job_listing_client_edit_form_fields", 'taxonomy_client_extra_field_url_display' );

add_action( 'edited_job_listing_client', 'taxonomy_client_extra_fields_save', 10, 2);
add_action( 'created_job_listing_client', 'taxonomy_client_extra_fields_save', 10, 2);

So I was able to figure out the problem I was having, thanks to @WisdmLabs for pointing me in the right direction.

Even though my custom post type had custom taxonomy, it was still using the original WordPress tables for posts and terms . My problem was that I was incorrectly using a custom name for $meta_type . The $meta_type variable is directly related to the table name that is used in update_metadata . This is why it wasn't saving in the database. It was trying to save it to a table called 'myCustomPostType_myCustomTaxonomy' . 'meta' 'myCustomPostType_myCustomTaxonomy' . 'meta' .

For the default taxonomy, WordPress is using the termmeta table, so I just had to change the following lines in both functions from this:

$meta_type = 'myCustomPostType_myCustomTaxonomy';

To this:

global $wpdb;
$meta_type = substr($wpdb->termmeta, strlen($wpdb->prefix), strlen('meta')); // equates to 'term'

Instead of just hardcoding $meta_type = 'term'; , I decided to take the actual wp_prefex_termmeta table that is used in the global $wpdb database object and work back from there to future-proof it in case the WP changes the word that is used for taxonomy tables.

Unfortunately, the meta suffix in WordPress is hardcoded in the version I'm using, WordPress 4.4. Doubt that will ever change anytime soon, though.

function custom_edit_tag_form_fields( $tag ) {
    global $post;
    $meta_key = 'subtitle';
    $post_id = $post->ID;
    $value   = get_post_meta( $post_id, $meta_key, true );    
?>
<tr class="form-field term-description-wrap">
    <th scope="row">
        <label for="subtitle">Subtitle</label>
    </th>
    <td>
        <input name="subtitle" id="subtitle" type="text" value="<?php echo $value; ?>" size="40" />
    </td>
</tr>   
<?php
}


function custom_edited_terms( $post_id ) {   
    $meta_key = 'subtitle';
    if( isset($_POST[$meta_key]) ) {
        $meta_value = esc_attr( $_POST[$meta_key] );
        update_post_meta( $post_id, $meta_key, esc_attr( $_POST[meta_key], true ) );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM