Support » Fixing WordPress » pulling 1 random post from a specific category

  • Resolved kerusdotorg

    (@kerusdotorg)


    i have one category set up for splash/entry pages on a project.
    the idea is to show one single random excerpt and nothing else on the index
    so far it has been cake:
    – i have the single cat,
    – single post,
    – nothing but excerpt

    the part i am missing is the randomization

    i tried this:
    <?php shuffle(query_posts("showposts=1&cat=1")); ?>

    i thought the shuffle function would randomize the query and it would pull the “top” of the randomized group. no dice. (thanks to whoami for the shuffle tidbit)

    i spent a good 3 hours searching on here, googling and trying to figure this out myself.. but i am throwing in the towel and groveling for your mercy /sadface

    any help would be greatly appreciated

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi, this might not match exactly what you need, but have you checked the Random Categories with Random Posts plugin here: http://www.screenflicker.com/blog/web-development/wordpress-plugin-random-categories-with-random-posts/ ?

    From what I read it you can specify what category you want as the source and how many post you want to be pulled (in your case, 1). Hope this helps..

    thank you for the link! definitely worth a try 🙂

    that plugin did not work out as planned..

    maybe i can make a custom function for this =o

    anyone have any suggestons?

    ah-ha! this works like a charm!

    http://wordpress.org/support/topic/91421?replies=15#post-532012
    naturally replace CATNAME with your cat name

    <?php
    global $wpdb;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = 'publish' and $wpdb->categories.cat_name = 'CATNAME' ORDER BY RAND() LIMIT 1");
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

    hope this helps someone else in the future

    notes: pull 1 random post or entry from single specific category

    ah-ha! this works like a charm!

    http://wordpress.org/support/topic/91421?replies=15#post-532012
    naturally replace CATNAME with your cat name

    <?php
    global $wpdb;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = 'publish' and $wpdb->categories.cat_name = 'CATNAME' ORDER BY RAND() LIMIT 1");
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

    hope this helps someone else in the future

    notes: pull 1 random post or entry from single specific category

    Does anyone know how to do this in 2.3?

    I guess this is it…

    <?php
    
    	global $wpdb;
    	$numposts = 1;
    	$rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts,$wpdb->terms,$wpdb->term_relationships WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id and $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id and $wpdb->posts.post_status = 'publish' and $wpdb->terms.name = 'CATNAME' ORDER BY RAND() LIMIT $numposts");
    	foreach($rand_posts as $post) :
    	setup_postdata($post);
    ?>

    Hope it helps.

    Jauhari

    (@jauhari)

    IT doesn’t work for WP 2.3 🙁

    What’s wrong? please give completed example

    Kafkaesqui

    (@kafkaesqui)

    http://codex.wordpress.org/Template_Tags/get_posts#Random_posts

    Just append the ‘category’ parameter to the arguments (read up from the example).

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘pulling 1 random post from a specific category’ is closed to new replies.