I need to assign a unique identifier string eg. 524bbc5a3771d to each and all posts (unique string for each post) in my site. So I tried to put together a code by picking up bits and pieces from here and there and was finally able to generate a string. Here is the code.

function unique_post_id_generation($post){
  global $post;
  $generated_id = uniqid();
          $update_query = new WP_Query('posts_per_page=-1');
          while ( $update_query->have_posts() ) : $update_query->the_post();
    add_post_meta($post->ID, 'unique_post_identifier', $generated_id, true);
add_action( 'init', 'unique_post_id_generation' );

The reason I used uniqid() instead of wp_rand() is that I found uniqid() generates alphanumeric value which I suppose is more diverse.

I have it echoed to my single.php as I needed, using: <?php echo get_post_meta($post->ID, 'unique_post_identifier', true);?>. It displays a nice randomn string eg. 524bbc5a3771d but the issue is, it is displaying the same string in all posts. Here are a two more things I request you to keep in mind while suggesting a correction :

  1. I hope that the generated correction will be non-repeated-unique-strings.
  2. I need a unique string to be generated only once for each post so the code above is using add_post_meta so please provide me something like above which only have to run once, but again feel free to suggest any other way that is better and more scalable (not resource hungry).
  3. I need the string to appear on posts only, nowhere else eg. attachment-pages etc.
  4. The string value should not change on any post-updates.

This must be something very basic, since I am at a learning stage and I could be wrong in any of my approach. Let me know if I should try an entirely different solution with an sql query as suggested here.

  • Not sure why you need the identifier, so there might be a good reason to have it, but on the other hand every post, that means everything that counts as such in wp, is uniquely identified by the ID. – Nicolai Oct 2 '13 at 11:56
  • You are correct and I am aware of that. But I need to generate an extra identification code other than the post id, which will be utilized by many other functions. I wanted to leave the ID mostly untouched, although I use it for some important functions. – gurung Oct 2 '13 at 12:16
  • Just to clarify if you use your additional unique identifier just to identify the post, as replacement for the post ID, I wouldn't recommend it because it's a absolutely unnecessary step. The only reason I can imagine to do this is to obscure the post ID by using the unique identifier in those other functions you are talking about. – Nicolai Oct 2 '13 at 12:57

There is a simplier way to do what you want to:

function add_unique_post_identifier( $post_id ) {

$unique_post_identifier = get_post_meta($post_id, 'unique_post_identifier', true);

// do nothing if post type is not 'post' or identifier is already set
if ('post' != get_post_type( ( $post_id ) ) || !empty($unique_post_identifier) )

    $generated_id = uniqid();

    update_post_meta($post_id, 'unique_post_identifier', $generated_id);

// run when post is created or updated
add_action( 'save_post', 'add_unique_post_identifier' ); 
  • ,as soon as I tried the code site went blank, any idea why? – gurung Oct 2 '13 at 8:48
  • Where do you put it? It should be in functions.php in your theme directory. – Max Yudin Oct 2 '13 at 9:31
  • Yes, there only – gurung Oct 2 '13 at 9:32
  • Do you have DEBUG on? Also I've updated the code. It was assigning the identifier for all types, not only 'post'. – Max Yudin Oct 2 '13 at 9:38
  • Yes, I have the debug ON, how is this affected by debug ? is my only option to put debug OFF to make this work ? – gurung Oct 2 '13 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.