2

I want to generate two random numbers above every post and save them in the database so that it won't change every time we see or refresh that page.

I've decided to put this php code inside my single.php file of my theme for the random number:

<?php echo rand(1000, 10000); ?>

and put this one for random decimal number:

<?php echo $rand = 0.1*rand(40,49)?>

but they generate random numbers between specified values every time the visitor sees the page. I want to know how to generate those random numbers and store them along with other contents of the page in the database.

5
  • Just out of curiosity, why do you need/want this? Commented Nov 5, 2014 at 18:30
  • well before that tell me what do you guess/think?
    – user51354
    Commented Nov 5, 2014 at 18:39
  • I don't know, maybe some kind of additional identification, but actually I have no clue, could be, you just like the randomness of things. Commented Nov 5, 2014 at 18:48
  • it's used for generating automated random snippet ratings for old and new posts.
    – user51354
    Commented Nov 5, 2014 at 19:03
  • ok, I see, at least my curiosity is somewhat satisfied Commented Nov 5, 2014 at 20:31

1 Answer 1

4

I guess you mean something like this:

if( function_exists( 'get_post_random_wpse' ) )
    echo get_post_random_wpse( 
        $post_id    = get_the_ID(), 
        $meta_key   = '_post_random', 
        $meta_value = rand( 1000, 10000 ) 
    ); 

where:

function get_post_random_wpse( $post_id = 0, $meta_key = '_post_random', $meta_value = 0 )
{
    if( ! ( $post_id > 0 && strlen( $meta_key ) > 0 ) )
        return 0;

    if( '' === ( $post_rand = get_post_meta( $post_id, $meta_key, true ) ) ) 
            update_post_meta( $post_id, 
                $meta_key, 
                $post_rand = $meta_value 
            );

    return $post_rand;
}

where we use the post meta to store the random value for each post.

Update:

A comment response: It sounds like you want to store two random numbers for each post: _post_random_int and _post_random_dec where you display/store them with:

if( function_exists( 'get_post_random_wpse' ) )
    echo get_post_random_wpse( 
        $post_id    = get_the_ID(), 
        $meta_key   = '_post_random_int', 
        $meta_value = rand( 1000, 10000 ) 
    ); 

and

if( function_exists( 'get_post_random_wpse' ) )
    echo get_post_random_wpse( 
        $post_id    = get_the_ID(), 
        $meta_key   = '_post_random_dec', 
        $meta_value = 0.1 * rand( 42, 49 ) 
    ); 

but you should avoid cowboy coding on a live site and test this on a dev (localhost) install.

Notice that the underscore in the meta key, makes the custom fields unaccessible from the backend UI. If you want to be able to modify the custom fields from the backend, don't use the underscore, use e.g. post_random instead of _post_random.

14
  • 1
    Thank you. the first solution worked fine. would that cause any trouble in the future to use the first solution?
    – user51354
    Commented Nov 5, 2014 at 17:03
  • And if i want to generate and store another random number on that page and it be decimal, should i do that by just replacing rand( 1000, 10000 ) with something like int mt_rand ( int $min , int $max ) ?
    – user51354
    Commented Nov 5, 2014 at 17:06
  • 1
    I updated the answer to make it more flexible, now you can just use the third input argument as your random value. You can also adjust the second argument to your needs, if you need to store another value.
    – birgire
    Commented Nov 5, 2014 at 17:20
  • 1
    I updated the answer.
    – birgire
    Commented Nov 5, 2014 at 18:02
  • 1
    Custom fields starting with an underscore are hidden from the UI, so if you want access to them always don't use an underscore for them. You can just unhide them temporarily, see my answer, the first part. @Saeedem Commented Nov 5, 2014 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.