admin管理员组文章数量:1287810
It's rather easy to update post meta_data using this:
update_post_meta( $post_id, 'status', 'active');
Since I was recommended to use custom taxonomy instead, I have registered and created a custom taxonomy 'status', by pasting the below code in my functions.php:
//* Add custom taxonomy 'status' *//
function wporg_register_taxonomy_status() {
$labels = array(
'name' => _x( 'Status', 'taxonomy general name' ),
'singular_name' => _x( 'Status', 'taxonomy singular name' ),
'search_items' => __( 'Search Status' ),
'all_items' => __( 'All Status' ),
'parent_item' => __( 'Parent Status' ),
'parent_item_colon' => __( 'Parent Status:' ),
'edit_item' => __( 'Edit Status' ),
'update_item' => __( 'Update Status' ),
'add_new_item' => __( 'Add New Status' ),
'new_item_name' => __( 'New Status Name' ),
'menu_name' => __( 'Status' ),
);
$args = array(
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'status' ],
);
register_taxonomy( 'status', [ 'post' ], $args );
}
add_action( 'init', 'wporg_register_taxonomy_status' );
Ok, so now I have my custom taxonomy 'status'.
I have gone to Posts > Status inside the wp dashboard and created two entries named:
Active
Inactive
both have the same slugs.
Active has term_id: 661 Inactive has term_id: 662
Inside my create custom post form I have added this line so that when the post is created, it would automatically set the taxonomy to 'active':
<input type="hidden" name="usp-taxonomy-status[]" value="661" />
So now I have all new posts with selected status taxonomy to 'active'.
My question is how to use update_term_meta()
to update my selection?
Or should I use wp_set_post_terms ();
?
In a nutshell I want something like this:
update_term_meta ($post_id, 'status', 'inactive');
//and at the same time
uncheck the 'status', 'active';
So that only one 'inactive' would be left selected.
Need help with that.
It's rather easy to update post meta_data using this:
update_post_meta( $post_id, 'status', 'active');
Since I was recommended to use custom taxonomy instead, I have registered and created a custom taxonomy 'status', by pasting the below code in my functions.php:
//* Add custom taxonomy 'status' *//
function wporg_register_taxonomy_status() {
$labels = array(
'name' => _x( 'Status', 'taxonomy general name' ),
'singular_name' => _x( 'Status', 'taxonomy singular name' ),
'search_items' => __( 'Search Status' ),
'all_items' => __( 'All Status' ),
'parent_item' => __( 'Parent Status' ),
'parent_item_colon' => __( 'Parent Status:' ),
'edit_item' => __( 'Edit Status' ),
'update_item' => __( 'Update Status' ),
'add_new_item' => __( 'Add New Status' ),
'new_item_name' => __( 'New Status Name' ),
'menu_name' => __( 'Status' ),
);
$args = array(
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'status' ],
);
register_taxonomy( 'status', [ 'post' ], $args );
}
add_action( 'init', 'wporg_register_taxonomy_status' );
Ok, so now I have my custom taxonomy 'status'.
I have gone to Posts > Status inside the wp dashboard and created two entries named:
Active
Inactive
both have the same slugs.
Active has term_id: 661 Inactive has term_id: 662
Inside my create custom post form I have added this line so that when the post is created, it would automatically set the taxonomy to 'active':
<input type="hidden" name="usp-taxonomy-status[]" value="661" />
So now I have all new posts with selected status taxonomy to 'active'.
My question is how to use update_term_meta()
to update my selection?
Or should I use wp_set_post_terms ();
?
In a nutshell I want something like this:
update_term_meta ($post_id, 'status', 'inactive');
//and at the same time
uncheck the 'status', 'active';
So that only one 'inactive' would be left selected.
Need help with that.
Share Improve this question edited Sep 16, 2021 at 13:31 robert0 asked Sep 16, 2021 at 13:15 robert0robert0 2032 silver badges11 bronze badges1 Answer
Reset to default 0Sorted using this:
To set to active:
<?php $post_id = 58;
$tag = array( 661 );
wp_set_post_terms( $post_id, $tag, 'status' ); ?>
To set to inactive:
<?php $post_id = 58;
$tag = array( 662 );
wp_set_post_terms( $post_id, $tag, 'status' ); ?>
本文标签: phpHow to update posts39 custom taxonomy selection
版权声明:本文标题:php - How to update posts' custom taxonomy selection? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741316159a2371927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论