• Resolved jasontwright

    (@jasontwright)


    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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    Thread Starter jasontwright

    (@jasontwright)

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

    Thread Starter jasontwright

    (@jasontwright)

    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);
    }?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Most Recent Post Titles on Static Page’ is closed to new replies.