最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Change custom post taxonomy values from front-end

programmeradmin2浏览0评论

I'm developing a ticketing system which already has the custom post type for the tickets with a "status" taxonomy. I have the tickets displayed in a table on the front-end and the status column has the status values in a dropdown with the current status selected by default. How can I change the status taxonomy on the back-end based on the selection of the dropdown on the front-end?

I'm developing a ticketing system which already has the custom post type for the tickets with a "status" taxonomy. I have the tickets displayed in a table on the front-end and the status column has the status values in a dropdown with the current status selected by default. How can I change the status taxonomy on the back-end based on the selection of the dropdown on the front-end?

Share Improve this question asked Jun 4, 2019 at 20:18 Creative CacheCreative Cache 34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

That would be achieved using the wp_set_post_terms function.

https://codex.wordpress/Function_Reference/wp_set_post_terms

You could run an ajax request on the change event of the dropdown to either wp-admin/admin-ajax.php or a custom JSON API endpoint (preferred but more work) with a payload like this

{
    new_status: 123
    post_id: 321
}

Ensure 100% that the user that is logged in has authorization to modify the post

$post = get_post( filter_var( $_POST['post_id'], FILTER_SANITIZE_NUMBER_INT ) );
if( /*Test User Auth*/ ){
    wp_set_post_terms( $post->ID, [ 
        filter_var( $_POST['new_status'], FILTER_SANITIZE_NUMBER_INT ) 
    ], 'status' );
}
发布评论

评论列表(0)

  1. 暂无评论