• Resolved Tom Nolan

    (@tom-nolan)


    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

Viewing 10 replies - 1 through 10 (of 10 total)
  • vtxyzzy

    (@vtxyzzy)

    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; ?>
    Thread Starter Tom Nolan

    (@tom-nolan)

    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

    vtxyzzy

    (@vtxyzzy)

    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.

    Thread Starter Tom Nolan

    (@tom-nolan)

    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!

    vtxyzzy

    (@vtxyzzy)

    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.

    Thread Starter Tom Nolan

    (@tom-nolan)

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

    Thread Starter Tom Nolan

    (@tom-nolan)

    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?

    vtxyzzy

    (@vtxyzzy)

    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
    Thread Starter Tom Nolan

    (@tom-nolan)

    thanks again and again! That’s it – spot on.

    Page is running perfect!

    or you can use

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

    and it will show the tags randomly.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Displaying all posts from a randomly selected tag’ is closed to new replies.