WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying all posts from a randomly selected tag (11 posts)

  1. Tom Nolan
    Member
    Posted 2 years ago #

    Hello

    I'm making a video blog for some friends and need to need to add a function which will display all the posts from a randomly selected tag/randomly generated tag search.

    So the idea would be you'd go to the page and it'd filter down to show all the posts in a random tag (i.e. "/tag/chicago/") and then when someone refreshes the page we'd see all the posts from another tag (i.e. "/tag/ice")

    I can find lots of info on displaying posts randomly from a given tag or category, but can't find the way to do the opposite!

    Any help much appreciated asap - I was hoping to do this for them quite quickly

  2. vtxyzzy
    Member
    Posted 2 years ago #

    Here is some sample code that should get you started:

    <?php $terms = get_terms('post_tag');
    $ndx = rand(0,count($terms) -1);
    $term = $terms[$ndx];
    // A new query on the random tag
    $my_posts = new WP_Query("tag=$term->slug&ignore_sticky_posts=1&posts_per_page=-1");
    if ($my_posts->have_posts()) :
       while ($my_posts->have_posts()) :
          $my_posts->the_post(); ?>
          <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
       <?php endwhile; ?>
    <?php endif; ?>
  3. Tom Nolan
    Member
    Posted 2 years ago #

    thanks for that, sorry to be slow, but how/where would I implement this? If i use it say on the tag.php page, should it just pick up a random post?

    thanks again

  4. vtxyzzy
    Member
    Posted 2 years ago #

    I would not use it in tag.php because you may want to search for the posts with a certain tag.

    Here is what I did in developing and testing the code. It may vary for your theme.

    I made a copy of page.php and named it randomterms.php.

    I added the lines at the top to make it a template:

    <?php
    /*
    Template Name: Random Terms
    */
    ?>

    I replaced the Loop code with the code I posted above, making sure that the php tags were correct.

    I created a new blank Page and assigned it the 'Random Terms' template.

    Each time the page is viewed or refreshed, a new term is chosen and the titles of posts with that tag are listed.

  5. Tom Nolan
    Member
    Posted 2 years ago #

    Brilliant, thanks so much! Works a treat

    I've replaced some of the code so now it's displaying 'the_content' and I have deleted the posts per page so that it displays all the posts it finds.

    Another aspect of the site is that I have all the posts split between the two authors - so for any given query there are two columns - one for one author, and the other for the second author. I can't figure out with this random tag query how I can then split within that into authors - I have quickly duplicated the code to run it twice and added the query to specify the author to each, but this is obviously displaying two different tag results.

    [code moderated - please use tha pastebin for any code over the forum limit of 10 lines]

    Can I call the tag query, then within that call two authors into separate columns? Or do I do two loops with the first calling a random tag and the second taking that random tag from the first?

    thanks again!

  6. vtxyzzy
    Member
    Posted 2 years ago #

    The 'quick and dirty' way to use the same tag is to change the second query from this:

    <?php $terms = get_terms('post_tag');
    $ndx = rand(0,count($terms) -1);
    $term = $terms[$ndx];
    // A new query on the random tag

    to this:

    <?php
    // A new query on the random tag

    The better way would be to use the author ids in the query and sort on author, but that code is too long to post here.

    EDIT: You might want to leave in the line that prints the linked title so a viewer can click on it to see the full post.

  7. Tom Nolan
    Member
    Posted 2 years ago #

    THANK YOU!!!! That's exactly what I needed.

  8. Tom Nolan
    Member
    Posted 2 years ago #

    promise this is the last question...

    is there a way to then exclude the last result on refresh, so I don't get the same two tags back to back?

  9. vtxyzzy
    Member
    Posted 2 years ago #

    You can use a SESSION variable by adding two lines at the top of the template:

    <?php
    /*
    Template Name: Random Terms
    */
    session_start();
    $term_id =  (isset($_SESSION['term-id'])) ? $_SESSION['term-id'] : '';
    ?>

    and changing this before the first query:

    $terms = get_terms('post_tag');
    $ndx = rand(0,count($terms) -1);
    $term = $terms[$ndx];
    // A new query on the random tag

    to this:

    $terms = get_terms('post_tag');
    for ($i=0; $i<5 ; ++$i ) {  // Only try 5 times
       $ndx = rand(0,count($terms) -1);
       $term = $terms[$ndx];
       if($term->term_id != $term_id) break;  // Not the same as the last
    }
    $_SESSION['term-id'] = $term->term_id;
    // A new query on the random tag
  10. Tom Nolan
    Member
    Posted 2 years ago #

    thanks again and again! That's it - spot on.

    Page is running perfect!

  11. smhz101
    Member
    Posted 1 year ago #

    or you can use

    $terms = get_terms('post_tag');
    shuffle($terms);

    and it will show the tags randomly.

Topic Closed

This topic has been closed to new replies.

About this Topic