如何使用自动递增编号更新自定义帖子类型的分类术语

时间:2017-10-02 作者:joe

我正在尝试做一些类似于这段代码的事情,但在分类法中使用税务术语,而不是在帖子中使用自定义字段

简而言之,我想在保存术语时自动增加自定义分类术语的分类术语中的一个简单数值,以便每次在cms中创建新术语时,都会自动为其分配比前一个术语更高的下一个数字-因此,我将以这样的税务术语列表结束

分类法:作业编号表格:1、2、3、4、5、6等

谢谢

/**
* Add an auto-incrementing Project ID field to Design feedback posts
*/
function auto_assign_ids( $post_id, $post, $update ) {

// Only assign ID to new design posts
if ( $post->post_status == \'publish\' && $post->post_type == \'designapprovalsystem\' ) {

    // get the most recent Project post
    $project_args = array(
        \'numberposts\'       =>   2,
        \'post_type\'         =>   \'designapprovalsystem\',
        \'orderby\'           =>   \'post_date\',
        \'order\'             =>   \'DESC\'
    );
    $projects = get_posts( $project_args );

    // get the project_id of the prior post
    //get the custom field value of a post
    $last_id = get_post_meta( $projects[1]->ID, \'job_number\', true );

    // increment
    $last_id++;

    // set the project_id of the current post
    update_post_meta( $post_id, \'job_number\', $last_id );
    }
 }
add_action( \'save_post\', \'auto_assign_ids\', 100, 3 );

1 个回复
SO网友:lukgoh

/**
* Add an auto-incrementing Project ID field to Design feedback posts
*/
function auto_assign_ids( $post_id, $post, $update ) {

// Only assign ID to new design posts
if ( $post->post_status == \'publish\' && $post->post_type == \'designapprovalsystem\' ) {

    // get the most recent Project post
    $project_args = array(
        \'numberposts\'       =>   2,
        \'post_type\'         =>   \'designapprovalsystem\',
        \'orderby\'           =>   \'post_date\',
        \'order\'             =>   \'DESC\'
    );
    $projects = get_posts( $project_args );

    // get the project_id of the prior post
    //get the custom field value of a post
    $last_id = get_post_meta( $projects[1]->ID, \'job_number\', true );

    // increment
    $last_id++;

     // set the project_id of the current post
     if ( !add_post_meta( $post_id, \'job_number\', $last_id, true ) ) { 

     update_post_meta( $post_id, \'job_number\', $last_id );

     }

    }
 }
add_action( \'save_post\', \'auto_assign_ids\', 100, 3 );
我不是百分之百确定,但我认为问题是页面还没有ID,所以你不能更新它,你必须先插入它。

结束

相关推荐

有没有办法在Custom Post Listing页面上重新排序Custom Taxonomy下拉菜单?

我用过register_taxonomy() 具有\'show_admin_column\' => true 成功获取自定义帖子类型列表页面上的下拉列表。默认情况下,下拉列表按ID顺序显示术语。有人知道把这个改成字母顺序的方法吗?