Forums

How do I fetch a post ID randomly from published posts? (2 posts)

  1. arretx
    Member
    Posted 1 year ago #

    I am currently using the Get-a-post plugin which allows me to put a single post in the header based on the post number I choose. This is what I have:

    <?php get_a_post(234); ?> which pulls the post with the_id() of 234. I then added the following code:

    <?php srand(time());
    $random = (rand()%319); ?>

    and replaced <?php get_a_post(234); ?> with <?php get_a_post($random); ?>

    This is all fine and dandy, but when the random number generator picks a number from a post that is not published and may or may not exist, the results are blank.

    Is there a way to define the array from which the random numbers can be chosen that is drawn directly from the database such that if a post is added or removed the script will adjust?

  2. Kafkaesqui
    Moderator
    Posted 1 year ago #

    Hmm. Why have I never added a random post option to Get-a-Post? Really, it seems like it should have been there since the beginning...

    http://guff.szub.net/2005/01/27/get-a-post/#update-20080127

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.