Support » Plugins » Hacks » Adding a Parent filter to my random page generation

  • Guys,

    Where am I going wrong here… I know its late as hell over here and I just hit a wall, I’ve goofed around with post_parent and I just cant get it right. Right now the code below generates a random page from all pages I’ve created when my button is clicked. What Im looking for is for the button to generate a random page just as it is now but from a list of the CHILD pages. So, basically, I need the code to tell me what the parent of the current page is, then extract the list of CHILD pages that are published, randomly select a page out of that table and then store it in the variable $randomPage. You can see I’ve got 80% of it done below. Can anyone help me out here? Check the code below.

    $randomPage = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY rand() LIMIT 1");
    echo '<a href="'.$randomPage.'"><img src="http://somesite/button.png"></a>';
Viewing 1 replies (of 1 total)
  • Hey, I tried to do a similar thing, where a link will take the user to a random page from the WordPress Site. I based it on your code and some code I found here.

    I added this to my functions.php file.

    function get_randompost() {
    	$sql = "SELECT ID FROM wp_posts WHERE post_type = 'page' AND post_status = 'publish' AND id <> 2 AND id <> 729 AND id <> 2724 AND id <> 2223 AND id <> 2814 AND id <> 738 AND id <> 744 AND id <> 747 AND id <> 751 AND id <> 753 AND id <> 775 AND id <> 889 AND id <> 973 AND id <> 997 AND id <> 1029 AND id <> 1106 AND id <> 1101 AND id <> 954 AND id <> 26 AND id <> 29 AND id <> 416 ORDER BY RAND() LIMIT 1";
        return mysql_result(mysql_query($sql), 0, 0);

    Then I used this link in a custom widget: (with some button styling)

    <a href="<?php echo get_randompost() ?>" class="small-button smallsilver"><span>Shuffle Knowledge</span></a>


    post_parent – I tried using this, and it worked, but it only gave me the child pages immediately under the post_parent ID I defined. What I needed was the is_tree function, but I didn’t know how to do that via SQL. I decided to just exclude the page IDs that i didn’t want to show up.

    <> – as you can see, I’m a novice, as I didn’t even know how to exclude an array of pages, I had to do it one by one! Ha!

Viewing 1 replies (of 1 total)
  • The topic ‘Adding a Parent filter to my random page generation’ is closed to new replies.