简体   繁体   中英

Last X posts in a database

I have this code which gets WP posts, but it gets all the posts and orders them from the first to the last. I want only the last 10 post ordered from last to the earlier. What do I have to change?

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'myblog';
mysql_select_db($dbname);

$result = mysql_query("SELECT * FROM wp_posts WHERE post_type = 'post'")



while($row = mysql_fetch_array($result))
{
echo $row['post_date'] . " " . $row['post_content'] . " " $row['post_title'];
echo "<br />";
}


?>

SELECT * FROM wp_posts WHERE post_type = 'post' ORDER BY post_date DESC LIMIT 10

Change your query...

SELECT * FROM wp_posts WHERE post_type = 'post' ORDER BY past_date DESC LIMIT 10;

Which orders posts from the newest to the oldest, limiting the results to only 10 rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM