WordPress.org

Ready to get started?Download WordPress

Forums

One Random Post (not random list or link) (24 posts)

  1. gvtexas
    Member
    Posted 9 years ago #

    I've searched through and can't seem to find this, although there are plugins for creating random posts, they make no sense (and have no details on how to implement them).

    I'm putting my blog in the morque, so all I want to do is make the front display one random post (1.5 allows one post, just not random).

    Thanks.

  2. Kafkaesqui

    Posted 9 years ago #

    Hmm. You can initialize The Loop to display a single post by inserting
    query_posts() just before it with an argument of "p=ID" -- ID being replaced with the numeric ID of a post. To pass it a random post ID requires a way of retrieving the ID randomly. So try this as your query_posts() code:

    <?php
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
    query_posts("p=$rand_id");
    ?>

  3. gvtexas
    Member
    Posted 9 years ago #

    Thanks, but I have no clue how to implement this. I tried this code, which results in no error, but no post shows either! Do I have it in the wrong place?

    <?php if (have_posts()) : ?>

    <?php<br />
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");<br />
    query_posts("p=$rand_id");<br />
    ?>

    <!-- ?php while (have_posts()) : the_post(); ? -->

    <div class="post"><br />
    <h2 id="post-<?php the_ID(); ?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/bug-head.jpg"> <a>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>

    <div class="entry">

    <?php the_content('Read the rest of this entry &raquo;'); ?><br />
    </div>

    <p class="postmetadata">posted <?php the_time('F jS, Y') ?> in <?php the_category(', ') ?> <strong>|</strong> <?php edit_post_link('Edit','','<strong>|</strong>'); ?> <?php comments_popup_link('no comments', '1 comment', '% comments'); ?> <strong>|</strong> <a>/wp-print.php?p=<?=the_ID()?>">print version</a>

    </div>

    <!-- ?php endwhile; ? -->

    <div class="navigation">

    <?php posts_nav_link('','','&laquo; earlier posts') ?><br />
    | <a>">home</a> | <?php posts_nav_link('','newer posts &raquo;','') ?>

    </div>

    <?php else : ?>

    <h2 class="center">Not Found</h2><br />
    <p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?><br />
    <?php include (TEMPLATEPATH . "/searchform.php"); ?>

    <?php endif; ?>

    </div><br />

  4. skippy
    Member
    Posted 9 years ago #

    Might I suggest my random posts plugin? It's a single template tag that you insert into your templates. You can select how many random posts to display (you want just one); as well as which categories to use (or zero for all categories).

    So, you could do this in your index.php:

    <?php
    $p = random_posts(0, 1);
    query_posts("p=$p");
    // run through the Loop as normal now

    See the query_posts() documentation for additional details.

  5. Ming
    Member
    Posted 9 years ago #

    You've got the following two lines commented out:
    <!-- ?php while (have_posts()) : the_post(); ? -->
    <!-- ?php endwhile; ? -->

    Those are the two lines that actually loop through and display each individual post. They should be:

    <?php while (have_posts()) : the_post(); ?>
    <?php endwhile; ?>

    That should get the posts displaying. Skippy's plugin also works quite well.

  6. Kafkaesqui

    Posted 9 years ago #

    My code would also go before The Loop, as I note above, and not *in* it:

    <?php
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
    query_posts("p=$rand_id");
    ?>
    <?php while (have_posts()) : the_post(); ?>

    ~Post-related code and template tags go here. ~

    <?php endwhile; ?>

  7. gvtexas
    Member
    Posted 9 years ago #

    Thanks to all. I got it working, Karfkaesqui, based on your last post suggestion. Works really well (now!). :-)

  8. wild26
    Member
    Posted 8 years ago #

    I'm trying to do this single, random-post thing too, but I can't get Skippy's plugin to work. When I place the few lines of code just above "the loop" I just get an error message.

    Do I have to comment out some other lines to make it work? Which ones?

  9. CounterDax
    Member
    Posted 8 years ago #

    I am using the code quite similar as described by Kafkaesqui above, however, it does show the entire content, without the <!--more--> tag and it neither shows the comment count. Anyway to implement this?

  10. CounterDax
    Member
    Posted 8 years ago #

    Oh yeah, I'm also trying to exclude a category... however just coding
    ... WHERE post_status = 'publish' AND post_category != 1 ...
    does not seem to work whatsoever. It actually only shows one post all the time that actually has that category number.

  11. Nathan
    Member
    Posted 8 years ago #

    What if I want a link that says "random" on my page and when you click it takes you to a random post or page. The above listed comments seem to deal with displaying in a template. I just want a link that jumps to a random page or post.

  12. CounterDax
    Member
    Posted 8 years ago #

    @ njg97r: just include the permalink instead of all the post data. Something like this should work for you:

    <?php
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
    query_posts("p=$rand_id");
    ?>
    <?php while (have_posts()) : the_post(); ?>

    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">random</a>

    <?php endwhile; ?>

    Yes, you load alot of the db, but since PHP and MySQL is so fast, that wouldn't be a problem at all. Everytime the page reloads, you'll get a new "random" link (basically, the permalink to the individual post).

  13. nandamai
    Member
    Posted 8 years ago #

    CounterDax: That code for just a random permalink is exactly what I was looking for. Works perfectly, too. Thanks!

  14. CounterDax
    Member
    Posted 8 years ago #

    I think my problem is solved... I should've called setup_postdata($post); too.

  15. Cregy
    Member
    Posted 8 years ago #

    Hi

    I have used the hack that Kafkaesqui proposed on this site:
    http://www.weddingcompanycornwall.co.uk
    It is the thing below the tips section. It is working brilliantly except for one thing. I only want to display part of the post and then link to the rest of the post. For the moment I am including the post title so that is a link, but how do I make those ... at the end of the post become a link please?

    Thanks

    Rich

  16. CounterDax
    Member
    Posted 8 years ago #

    Well, first of all, the link leads to a completely unrelated website: you trying to sneak in some advertisement?

    Can you post the snippet of code here? I recon the '...' in your post means the '[...]' when calling the excerpt... are you calling the excerpt? If this is the case you need to hack WordPress itself.

    /wp-includes/functions-formatting.php
    Locate:

    function wp_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
    $text = $post->post_content;
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = 55;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '[...]');
    $text = implode(' ', $words);
    }
    }
    return $text;
    }

    Change:

    function wp_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
    $text = $post->post_content;
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = 55;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '
    <a href="' . get_permalink() . '">...</a>');
    $text = implode(' ', $words);
    }
    } else {
    $text = $text . '... <a href="' . get_permalink() . '">more</a>&raquo;';
    }
    return $text;
    }

  17. JohnHeart
    Member
    Posted 7 years ago #

    Using this code below:

    <?php
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1");
    query_posts("p=$rand_id");
    ?>
    <?php while (have_posts()) : the_post(); ?>

    ~Post-related code and template tags go here. ~

    <?php endwhile; ?>

    How can I display more than one post? Thanks.

  18. JohnHeart
    Member
    Posted 7 years ago #

    Repeat the codes above many times?

  19. Chris_K
    Member
    Posted 7 years ago #

    Change "LIMIT 1" to "LIMIT 4" if you want 4 of 'em.

  20. JohnHeart
    Member
    Posted 7 years ago #

    I have changed it to LIMIT 4 but it only shows me 1 post.

  21. Kafkaesqui

    Posted 7 years ago #

    That's because:

    $wpdb->get_var();

    will grab only one variable (the ID of the randomly selected post), and:

    query_posts("p=$rand_id");

    collects just the one post. To display any number of random posts:

    http://wordpress.org/support/topic/91421#post-462158

    Which is on the first page of results for:

    http://wordpress.org/search/random+posts?forums=1

    For that last link: I'm just saying...

  22. JohnHeart
    Member
    Posted 7 years ago #

    The link:

    http://wordpress.org/support/topic/91421#post-462158

    is good. How can I specify the category? How can I insert this two codes:

    1. $now = date('Y-m-d') . ' 23:59:59';
    2. AND (post_end_date >= '$now' OR post_end_date IS NULL)

  23. JohnHeart
    Member
    Posted 7 years ago #

    This is the modified code and it works except that pages and drafts also show in random posts. Anyone knows how to exclude it? Thanks.


    <?php
    global $wpdb;
    $numposts = 3;
    $now = date('Y-m-d') . ' 23:59:59';
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_end_date >= '$now' OR post_end_date IS NULL ORDER BY RAND() LIMIT $numposts");
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>
    ~Post-related code and template tags go here. ~
    <?php endforeach; ?>

  24. madadam
    Member
    Posted 7 years ago #

    It looks like the post_category is not used in the posts table anymore (if it ever did). instead you need to join with the post2cat table. The code below grabs 2 random posts from category 2.

    <?php
    global $wpdb;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts AS p, $wpdb->post2cat AS c WHERE p.ID = c.post_id AND c.category_id = 2 AND p.post_status = 'publish' ORDER BY RAND() LIMIT 2");
    foreach ($rand_posts as $post) :
    setup_postdata($post);
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags