• Swennet

    (@swennet)


    Hello everyone, I currently have a button on my website that shows a random article when the users clicks on it. However I recently found out that some of the posts are from a category that I don’t want to be shown.

    My question is, how do I alter this code so that it only shows posts from a certain category?

    $randomPost = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY rand() LIMIT 1");

    Thanks for the help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • pixelrow

    (@pixelrow)

    Please paste the full code of the button – might be able to help further. Cannot tell from the above. Also what is your site link

    Thread Starter Swennet

    (@swennet)

    <?php
    			$randomPost = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY rand() LIMIT 1");
    			echo '<a href="'.$randomPost.'">BUTTON</a>';
    		?>

    Website link is http://www.CreepypastaIndex.com

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Link to random post in Category’ is closed to new replies.