WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Recent Posts hack broken by images in wp_posts (3 posts)

  1. chiquita
    Member
    Posted 8 years ago #

    On my blog at http://www.escapecrate.co.uk I've been using a really bad, hacky way to pull out my last 3 posts at the bottom of my page.

    I searched for a plugin to do this, but I could never find one. So I hacked one together using my really basic knowledge of PHP and MySQL.

    It wasn't pretty, but it worked pretty effectively until I upgraded to 2.0 and uploaded images.

    Because Images now get stored in the wp_posts table all my crappy script is pulling out is the last three images I uploaded.

    I can see that I could filter out the images by using the post_staus field, but my lack of PHP knowledge means I'm not sure how to do it.

    At the moment my code is like this:


    $query="SELECT * FROM wp_posts ORDER BY post_date DESC";
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    $num = mysql_num_rows($result);
    $limit = "4";


    if ($num>$limit) {
    $to=$limit;
    }else{
    $to=$num;
    }


    if ($num > 0) {

    $i = 0;
    while ($i < $to) {

    $post_title=mysql_result($result,$i,"post_title");

    $post_content=mysql_result($result,$i,"post_content");
    $limit = "200";
    $post_length = strlen($post_content);

    How/where would I add a bit that says:

    if post_status = attatchment then goto next record?

    Or am I barking up the complete wrong tree?

  2. chiquita
    Member
    Posted 8 years ago #

    I've worked it out now, I just needed to change the first line to

    $query="SELECT * FROM wp_posts WHERE post_status='publish' ORDER BY post_date DESC";

  3. kickass
    Member
    Posted 8 years ago #

    Wouldn't it be easier to just use the proper template tag? It's certainly a lot LESS lines of code--

    <?php wp_get_archives('type=postbypost&limit=10'); ?>

    This tag just pulls post titles, but I'm sure it can be altered to pull the posts.

Topic Closed

This topic has been closed to new replies.

About this Topic