The MU forums have moved to WordPress.org

Crazy term IDs (2 posts)

  1. re5et
    Member
    Posted 18 years ago #

    I have noticed that lately any time I add a category or a tag through the admin, it ends up with a nutty, exponentially larger term_id in $wpdb->terms, like so:

    |      48 |
    |      49 |
    |      50 |
    |      51 |
    |      52 |
    |     847 |
    |    3797 |
    |    5678 |
    |    9063 |
    |   11852 |
    |   12200 |
    |   14519 |
    |   14797 |
    |   18097 |
    |   19058 |
    |   20353 |
    |   22471 |
    |   23128 |

    The jump to a 5 and soon 6 digit ID is happening a little too quickly for comfort. Curious as to why this was happening, i tracked the code to the function "wp_insert_term" in the file "wp-includes/taxonomy.php". The cause seems to be lines 942-944:

    $maxterm = $wpdb->get_var( "SELECT max(term_id) FROM {$wpdb->terms}" );
    $term_id = mt_rand( $maxterm+100, $maxterm+4000 );
    $wpdb->query("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES ('$term_id', '$name', '$slug', '$term_group')");

    Why is it that a crazy number has to be generated for the insert instead of just incrementing?

  2. re5et
    Member
    Posted 18 years ago #

    I changed:

    $maxterm = $wpdb->get_var( "SELECT max(term_id) FROM {$wpdb->terms}" );
    $term_id = mt_rand( $maxterm+100, $maxterm+4000 );

    to

    $maxterm = $wpdb->get_var( "SELECT max(term_id) FROM {$wpdb->terms}" );
    $term_id = ++$maxterm;

    So that the increment works as expected, is there any reason not to do this, will it screw up anything else?

About this Topic