在wp_INSERT_POST上创建唯一字母数字ID

时间:2017-10-03 作者:Game Unity

我需要一个随机的(唯一的)字母数字ID,用于永久链接和多站点安装之间的post关系。我的想法是在wp_insert_post 并将其另存为中的自定义字段值_alphanumeric_id.

我已经试着自己解决了这个问题,但我犯了500个错误:

function generateAlphanumericID( $length = 11, $post_id ) {

    $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ-_~\'!,";
    $charactersLength = strlen($characters);
    $randomString = \'\';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    /**
     * Now check here if the string is in the database
     * I am using two custom functions is_AlphanumericID_available() and save_AlphanumericID_into_db()
     */
    if( is_AlphanumericID_available( $randomString ) ){
        save_AlphanumericID_into_db( $randomString, $post_id );
        return $randomString;
    } else {
        return generateAlphanumericID();
    }
}
add_action(\'wp_insert_post\',\'generateAlphanumericID\'10, 3);



// function to grab all possible meta values of the chosen meta key.
function get_meta_values( $meta_key,  $post_type = \'post\' ) {

    $posts = get_posts(
        array(
            \'post_type\' => $post_type,
            \'meta_key\' => $meta_key,
            \'posts_per_page\' => -1,
        )
    );

    $meta_values = array();
    foreach( $posts as $post ) {
        $meta_values[] = get_post_meta( $post->ID, $meta_key, true );
    }

    return $meta_values;

}

// function to check if the randomString is available
function is_AlphanumericID_available( $randomString ) {

    $arrayString = get_meta_values( \'_alphanumeric_id\', \'article\' );


    if (in_array( $randomString, $arrayString)) {
      return false; // "Match found"
    }  else {
      return true;  // "Match not found"
    }


}



// function to save the randomString as custom field value
function save_AlphanumericID_into_db( $randomString, $post_id ) {

    add_post_meta($post_id, \'_alphanumeric_id\', $randomString);
}
add_action(\'wp_insert_post\', \'save_AlphanumericID_into_db\', 10, 3);
更新05。好的。2017年:
function save_AlphanumericID_into_db( $post_id ) {

    $randomString = \'Y-7gd789ovW\';


    // check if the randomString is available
    $posts = get_posts( array( 
        \'post_type\' => \'article\',
        \'meta_key\' => \'_alphanumeric_id\', 
        \'posts_per_page\' => -1,
        )
    );  

    $meta_values = array();
    foreach( $posts as $post ) {
        $meta_values[] = get_post_meta( $post->ID, $meta_key, true );
    }

    if (in_array( $randomString, $arrayString)) {
        // "Match found"
         add_post_meta($post_id, \'_alphanumeric_id\', \'error\');

    }  else {
        // "Match not found"
        add_post_meta($post_id, \'_alphanumeric_id\', $randomString);
    }





}
add_action(\'wp_insert_post\', \'save_AlphanumericID_into_db\', 10, 3);
这项工作(作为单一功能)没有错误,但db检查不起作用-如果密钥已经存在,我没有添加“错误”。-知道为什么吗

1 个回复
最合适的回答,由SO网友:Game Unity 整理而成

请参见此处的工作版本

// function to save the randomString as custom field value
function generate_AlphanumericID( $post_id ) {

    $postTypes = array(\'profile\', \'article\');
    $postType = get_post_type( $post_id );

    if (in_array( $postType, $postTypes ) ) {


        $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ-_~\'!,";

        $charactersLength = strlen($characters);
        $randomString = \'\';
        for ($i = 0; $i < 11; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }

         /**
         * Now check here if the string is in the database
         */
        $args = array(
        \'post_type\'     =>  array(
                $postTypes 
            ),
        \'meta_query\'    =>  array(
            array(
                \'meta_key\'  =>  \'_alphanumeric_id\'
            )
        )
        );
        $posts = new WP_Query( $args );


        $meta_values = \'\';
        if( $posts->have_posts() ) {
          while( $posts->have_posts() ) {
            $posts->the_post();

            $meta_values[] = get_post_meta( get_the_ID(), \'_alphanumeric_id\', true );
          }
        } 
        wp_reset_postdata();


        if (in_array( $randomString, $meta_values )) {
            // "Match found"
            return generate_AlphanumericID;

        }  else {
            // "Match not found"
            add_post_meta($post_id, \'_alphanumeric_id\', $randomString);
            return $randomString;
        }

    }

}
add_action(\'draft_to_publish\', \'generate_AlphanumericID\', 10, 3);
注意:要将此键用作永久链接标记,我建议您https://github.com/athlan/wordpress-custom-fields-permalink-plugin

结束