我正试图为WordPress中的自定义帖子类型在我的自定义分类术语中添加一个自定义元数据字段。基本上,我想为我的文章类型在分类中添加一个副标题字段。
通过挂接edit_tag_form_fields的操作,我可以将自定义字段添加到编辑表单中。
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
}
为了保存元数据,我在edited_terms
操作中添加了一个钩子,用于收集和保存输入的数据。
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);
}
}
我不太清楚为什么它没有被存储到数据库中。如果我回显$value
,我会得到一个空字符串。我希望它是我在输入字段中键入的值。
您读过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);
所以我能够解决我遇到的问题,感谢@WisdmLabs为我指明了正确的方向。
尽管我的自定义帖子类型有自定义的分类法,但它仍然使用posts
和terms
的原始WordPress表。我的问题是我错误地使用了$meta_type
的自定义名称。$meta_type变量与update_metadata
中使用的表名直接相关。这就是为什么它没有保存在数据库中。它正试图将其保存到一个名为'myCustomPostType_myCustomTaxonomy' . 'meta'
的表中。
对于默认的分类法,WordPress使用termmeta
表,所以我只需要更改这两个函数中的以下行:
$meta_type = 'myCustomPostType_myCustomTaxonomy';
对此:
global $wpdb;
$meta_type = substr($wpdb->termmeta, strlen($wpdb->prefix), strlen('meta')); // equates to 'term'
我不只是对$meta_type = 'term';
进行硬编码,而是决定使用全局$wpdb
数据库对象中使用的实际wp_prefex_termmeta
表,并从那里返回以进行将来的验证,以防WP更改用于分类表的单词。
不幸的是,WordPress中的meta
后缀在我使用的版本WordPress 4.4中是硬编码的。不过,怀疑这种情况是否会很快改变。
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 ) );
}
}