Skip to:
Content
Pages
Categories
Search
Top
Bottom

latest wordpress stories (code)


  • Tom(dB)
    Participant

    @tomdebruin

    Just made a query in my forum functions.php to display the 5 latest stories from WordPress. Pretty basic but it may help someone:

    // get WP news
    function wpnews() {
    global $bbdb;
    $stories = $bbdb->get_results("SELECT post_title, guid, post_date_gmt FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date_gmt DESC LIMIT 0,4");
    foreach ($stories as $story) {
    if ($i <= 4) {
    echo '<li><a href="' . $story->guid . '">' .$story->post_title . '</a></li>';
    $i++;
    }
    }
    }

    then call it on your forum with <?php wpnews() ?> and style to suit.

Viewing 5 replies - 1 through 5 (of 5 total)
  • FYI, this only lists 4 posts but is FREAKIN AWESOME! :D


    _ck_
    Participant

    @_ck_

    FYI, the if ($i <= 4) { is not needed because you already do a LIMIT on the query.

    Related topic:

    http://bbpress.org/forums/topic/heres-how-to-show-bbpress-info-inside-wordpress-without-full-integration


    Tom(dB)
    Participant

    @tomdebruin

    On my version I’m fetching 10 latest rows, randomizing, then displaying 5 stories.

    $bbdb->get_results("SELECT post_title, guid FROM site_posts WHERE post_type='post' AND post_status='publish' ORDER BY rand() LIMIT 0,10");

    Admittedly, I didn’t make the correct alterations to the previous example. Anyway, it should give people an idea of what to do.

    oh, and _ck_ I hadn’t spotted your earlier post – it would have made it a lot easier for me to figure out if I had!


    Tom(dB)
    Participant

    @tomdebruin

    and my final correction on the random thing!

    $stories = $bbdb->get_results("SELECT post_title, guid FROM site_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date_gmt DESC LIMIT 0,10");
    shuffle($stories);

    previous example was getting a random 10 from all posts.

    This get’s 10 latest posts, then randomizes them with the ‘shuffle’.

    cheers!


    _ck_
    Participant

    @_ck_

    Yup “order by rand()” is very handy.

    I used it for a mod for WordPress years ago to make a “random” category which is very useful.

    It could even be done to make a “random topics” view in bbPress but not sure how useful that would be.

    It’s fine for casual use but keep in mind for larger tasks it’s very slow and makes mysql purists shudder.

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.