WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Most Recent Post Titles on Static Page (4 posts)

  1. jasontwright
    Member
    Posted 1 year ago #

    I am trying to display the titles of my most recent posts in the sidebar of a static web page. For whatever reason, it is displaying the oldest post first while I want the newest post first in the list. Below is the code.

    <?php
    $how_many=5; //How many posts do you want to show
    require_once("./news/wp-config.php"); // Change this for your path to wp-config.php file?>
    
    <ul>
    <?
    $news=$wpdb->get_results("SELECT 'ID','post_title' FROM $wpdb->posts WHERE 'post_status'= \"publish\" ORDER BY 'ID' ASC LIMIT ".$how_many);
    foreach($news as $np){
    printf ("<li><a href=\"/news/index.php?p=%s\">%s</a></li>", $np->ID,$np->post_title);
    }?>
    </ul>

    Any help would be much appreciated!

  2. jonradio
    Member
    Posted 1 year ago #

    You have ASC in the SQL SELECT statement. Which means the lowest numbered ID is displayed first (ASCending). The lowest numbered ID is also the oldest.

  3. jasontwright
    Member
    Posted 1 year ago #

    Thanks for the quick response! However, I have tried both ASC and DESC to no avail.

  4. jasontwright
    Member
    Posted 1 year ago #

    Looks like the following code works:

    <?php
    $how_many=5; //How many posts do you want to show
    require_once("./news/wp-config.php"); // Change this for your path to wp-config.php file?>
    <ul>
    <?
    $news=$wpdb->get_results("SELECT 'ID', 'post_title', 'post_date' FROM $wpdb->posts
    WHERE 'post_status'= \"publish\" ORDER BY ID DESC LIMIT $how_many");
    foreach($news as $np){
    printf ("
    <li><a href="/news/index.php?p=%s">%s</a></li>
    ", $np->ID,$np->post_title);
    }?>

Topic Closed

This topic has been closed to new replies.

About this Topic