开发者

How do I simply retrieve the last most recent publish post in wordpress with mysql?

开发者 https://www.devze.com 2022-12-11 16:48 出处:网络
I\'m looking to retrieve the most recently published post in a wordpress system. I have somebasic mysql understanding and I can find where the revisions are stored in the wp_posts table but what I can

I'm looking to retrieve the most recently published post in a wordpress system. I have some basic mysql understanding and I can find where the revisions are stored in the wp_posts table but what I can't seem to find is how to retrieve the permalink for the most recent post.

any ideas on how to retrieve the most recent post with mysql and permalink?

I see there are some existing functions from WP like so:

// get the latest blog en开发者_高级运维try
$myposts = get_posts('numberposts=1');
foreach($myposts as $post) :

    echo '<a href="' . the_permalink() . '">' . the_title() . '</a>';
endforeach;

But when I put this on a custom page I'm working on, it seems to just be pulling out the page name I'm currently on and the link to this page (even though I'm thinking the above function should be retrieving a 'post'.

What am I missing?


Just in case you may want to have a real MySQL solution, here's what I use:

$query = "SELECT * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 1";
$post = mysql_fetch_assoc(mysql_query($query));

now the $post array holds all the data regarding to the latest post.


Worked out the solution:

<?php

global $post; // needed this

// get the latest blog entry
$myposts = get_posts('numberposts=1&orderby=date&order=DESC'); // and more stuff here

foreach($myposts as $post) :

?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消