0

I have this code that generates a link to a random post in my blog:

<?php 

$posts = get_posts('orderby=rand&numberposts=1'); 

foreach($posts as $post): ?>

    <a href="<?php the_permalink(); ?>" title="Random Post from Our Blog" style="float:right;" class="random-widget">
        <span class="fa-random" style="font-family:FontAwesome;float:right;"></span>
    </a> 

<?php endforeach; 

wp_reset_postdata(); ?>

I replaced wp_reset_query() with wp_reset_postdata() after reading this question but it's not working for me. All of my pages are displaying random post content instead of the page content. This is called in my action bar, above the menu.

Update

After several iterations, my code now closely resembles that found in the Codex and I'm still experiencing the same problem. Here's what I've got so far:

<?php
    global $post;

    $args = array('orderby'=>'rand','numberposts'=>'1','offset'=>'0');
    $posts = get_posts($args);

    foreach($posts as $post): setup_postdata($post); ?>
        <a href="<?php the_permalink(); ?>" title="Random Post from Our Blog" style="float:right;" class="random-widget">
            <span class="fa-random" style="font-family:FontAwesome;float:right;"></span>
        </a>

    <?php endforeach;
    wp_reset_postdata(); ?>
  • No dice. See update. – KingRichard May 9 '16 at 22:08
  • Have you tried to replace numberposts with posts_per_page? – N00b May 9 '16 at 22:09
  • Still doesn't work. The problem isn't with getting the random post, that part works fine. The problem is that it's not returning to the normal Loop flow to display the correct page content after this random link is generated. – KingRichard May 9 '16 at 22:11
  • It shouldn't, get_posts doesn't modify global variables and should be safe to use anywhere. The question that you linked is using query_posts which does and should be avoided at any cost. – N00b May 9 '16 at 22:17
  • Figured it out. See my answer if you're interested. – KingRichard May 9 '16 at 22:24
1

Ok, thanks to The Maniac for all the help troublshooting.

Looks like I had to take this completely outside the loop and call the WP_Query class on a new variable to make it happen.

Here's what worked:

<?php
$query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );

while ( $query->have_posts() ) : $query->the_post(); ?>

    <a href="<?php the_permalink(); ?>" title="Random Post from Our Blog" style="float:right;" class="random-widget">
        <span class="fa-random" style="font-family:FontAwesome;float:right;"></span>
    </a>

<?php endwhile;
wp_reset_postdata(); ?>
|improve this answer|||||
  • Right on. I learned something new today too! – The Maniac May 9 '16 at 22:25
  • Well done for solving it :) WP_Query is a pretty powerful and customisable class, it's got a good reference at the Codex if you ever need to customise this again in the future. – Tim Malone May 9 '16 at 22:26
0

I think you just need to add a call to setup_postdata. Otherwise your code looks like it should work as intended:

<?php foreach($posts as $post): setup_postdata($post); ?>
    <a href="<?php the_permalink(); ?>" title="Random Post from Our Blog" style="float:right;" class="random-widget"><span class="fa-random" style="font-family:FontAwesome;float:right;"></span></a>
<?php endforeach; wp_reset_postdata(); ?>

(Note: I used colons instead of brackets, its easier to follow in my opinion but completely unnecessary)

|improve this answer|||||
  • Same problem. The random post in the link is the same post it displays on that page. – KingRichard May 9 '16 at 21:40
  • Odd, are you 100% sure that this code is causing the issue? I don't mean to be mean -- it just seems like something else is messing with the query. Can you try removing it completely just as a test? – The Maniac May 9 '16 at 21:46
  • Yes, removing it makes my pages work as they should. With this code in place, the post selected for the random link is the same displayed on the current page (any page gets a random post instead of it's content). – KingRichard May 9 '16 at 21:47
  • How about adding global $post; before the foreach? – The Maniac May 9 '16 at 21:50
  • 1
    Yep, it was just a shot in the dark thinking maybe you're changing the global post reference when you call wp_setup_postdata. I'm going through the get_posts documentation trying to find examples, that's where I got the idea from. – The Maniac May 9 '16 at 21:56

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.