WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] pulling 1 random post from a specific category (10 posts)

  1. kerusdotorg
    Member
    Posted 6 years ago #

    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

  2. Hafiz Rahman
    Member
    Posted 6 years ago #

    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..

  3. kerusdotorg
    Member
    Posted 6 years ago #

    thank you for the link! definitely worth a try :)

  4. kerusdotorg
    Member
    Posted 6 years ago #

    that plugin did not work out as planned..

    maybe i can make a custom function for this =o

    anyone have any suggestons?

  5. kerusdotorg
    Member
    Posted 6 years ago #

    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

  6. kerusdotorg
    Member
    Posted 6 years ago #

    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

  7. kartlink
    Member
    Posted 6 years ago #

    Does anyone know how to do this in 2.3?

  8. cebradesign
    Member
    Posted 6 years ago #

    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.

  9. Jauhari
    Member
    Posted 6 years ago #

    IT doesn't work for WP 2.3 :(

    What's wrong? please give completed example

  10. Kafkaesqui

    Posted 6 years ago #

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

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

Topic Closed

This topic has been closed to new replies.

About this Topic