Php 向WordPress添加自定义分类元数据字段
我正试图为WordPress中的自定义帖子类型向自定义分类术语中添加自定义元数据字段。基本上,我想为我的帖子类型在类别中添加一个副标题字段 我可以通过连接到edit_tag_form_fields'操作将自定义字段添加到我的编辑表单中Php 向WordPress添加自定义分类元数据字段,php,wordpress,Php,Wordpress,我正试图为WordPress中的自定义帖子类型向自定义分类术语中添加自定义元数据字段。基本上,我想为我的帖子类型在类别中添加一个副标题字段 我可以通过连接到edit_tag_form_fields'操作将自定义字段添加到我的编辑表单中 function custom_edit_tag_form_fields( $tag ) { $meta_type = 'myCustomPostType_myCustomTaxonomy'; $object_id = $tag->term_
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
}
我不太清楚为什么它没有被存储到数据库中。如果我回显$value
,则得到一个空字符串。我希望它是我在输入字段中键入的值。函数自定义\编辑\标记\表单\字段($tag){
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 ) );
}
}
全球$员额;
$meta_key='subtitle';
$post_id=$post->id;
$value=get\u post\u meta($post\u id,$meta\u key,true);
?>
字幕
功能自定义\编辑\标签\表单\字段($tag){
全球$员额;
$meta_key='subtitle';
$post_id=$post->id;
$value=get\u post\u meta($post\u id,$meta\u key,true);
?>
字幕
所以我能够找出我遇到的问题,感谢你为我指明了正确的方向
尽管我的自定义帖子类型具有自定义分类法,但它仍然使用原始WordPress表来处理帖子
和术语
。我的问题是我错误地使用了$meta\u type
的自定义名称。$meta\u type变量与更新元数据
中使用的表名直接相关这就是为什么它没有保存在数据库中。它试图将它保存到一个名为“myCustomPostType\u myCustomTaxonomy”的表中。“meta”
对于默认分类法,WordPress使用的是termmeta
表,因此我只需在此基础上更改两个函数中的以下行:
$meta_type = 'myCustomPostType_myCustomTaxonomy';
为此:
global $wpdb;
$meta_type = substr($wpdb->termmeta, strlen($wpdb->prefix), strlen('meta')); // equates to 'term'
我决定使用全局$wpdb
数据库对象中使用的实际wp\u prefex\u termmeta
表,而不是硬编码$meta\u type='term';
,并从那里返回,以防wp更改用于分类表的单词
不幸的是,WordPress中的meta
后缀在我使用的版本WordPress 4.4中是硬编码的。不过,我怀疑这种情况会很快改变。所以我能够解决我遇到的问题,感谢你为我指明了正确的方向
尽管我的自定义帖子类型具有自定义分类法,但它仍然使用原始WordPress表来处理帖子
和术语
。我的问题是我错误地使用了$meta\u type
的自定义名称。$meta\u type变量与更新元数据
中使用的表名直接相关这就是为什么它没有保存在数据库中。它试图将它保存到一个名为“myCustomPostType\u myCustomTaxonomy”的表中。“meta”
对于默认分类法,WordPress使用的是termmeta
表,因此我只需在此基础上更改两个函数中的以下行:
$meta_type = 'myCustomPostType_myCustomTaxonomy';
为此:
global $wpdb;
$meta_type = substr($wpdb->termmeta, strlen($wpdb->prefix), strlen('meta')); // equates to 'term'
我决定使用全局$wpdb
数据库对象中使用的实际wp\u prefex\u termmeta
表,而不是硬编码$meta\u type='term';
,并从那里返回,以防wp更改用于分类表的单词
不幸的是,WordPress中的
meta
后缀在我正在使用的版本WordPress 4.4中是硬编码的。不过,我怀疑这种情况会很快改变。你读过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);
函数分类法\u客户端\u额外字段\u url\u显示($client){
$client\u url=get\u term\u meta($client->term\u id,'client url',true);
?>
你读过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);
函数分类法\u客户端\u额外字段\u url\u显示($client){
$client\u url=get\u term\u meta($client->term\u id,'client url',true);
?>
这里,您使用update_metadata()更新了值,它使用_get_meta_table()检索表名来存储值未找到表,则返回false,并且没有用于自定义元类型的元表。您可以参考:@WisdmLabs,谢谢您提供更多信息。我将查看这些项目。我意识到我使用的核心函数不是为主题等设计的,但我只是尝试构建一个自定义应用程序作为学习资源。我想你对表的看法可能是对的。谢谢@WisdmLabs。我能够解决你的问题。在这里,你使用update_metadata()更新了值,它使用_get_meta_table()检索表名来存储值。如果_get_meta_table()未找到表,则返回false,并且没有用于自定义元类型的元表。您可以参考:@WisdmLabs,谢谢您提供更多信息。我将查看这些项目。我意识到我使用的核心函数不是为主题等设计的,但我只是尝试构建一个自定义应用程序作为学习资源。我想你对表格的看法可能是对的。谢谢@WisdmLabs。我能够解决我对你指导的问题。另请参阅