• Hi, These forums are great. I’ve searched thoroughly and using code I found on these pages implemented a random post on my “home” page.

    I have 2 posts “in the loop” and then the random post. I want to have the random post NOT be the same as either of the previous two. Say, have the random one be older than 7 days.

    I’ve done this:

    <?php
    $rand_id = $wpdb->get_var(“SELECT ID FROM $wpdb->posts WHERE post_status = ‘publish’and post_date < NOW() ORDER BY RAND() LIMIT 1”);
    query_posts(“p=$rand_id”);
    ?>

    I think the < NOW() keeps the post from being from today. But how do I change it so the post is AT LEAST 7 days old OR MORE?

    Thanks in advance,
    bboldb

Viewing 1 replies (of 1 total)
  • Thread Starter bboldb

    (@bboldb)

    Okay, I think I figured it out.

    I adapted this code presented here straight from https://codex.www.ads-software.com/The_Loop

    Step 2. The second loop, get the X latest posts (except one).

    The following code gets X recent posts (as defined in WordPress preferences) save the one already displayed from the first loop and displays them according to Do stuff.

    <?php if (have_posts()) : while (have_posts()) : the_post();
    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    <!– Do stuff… –>
    <?php endwhile; endif; ?>
    ——————————————–

    This will keep the random post from being the same as at least one other from the loop. *I think*

    I wonder why no one wants to help me?

    Maybe this will help someone else….

Viewing 1 replies (of 1 total)
  • The topic ‘Help with using NOW()’ is closed to new replies.