homepage Welcome to WebmasterWorld Guest from 54.166.113.249
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
query for mysql like latest post of WP
query for mysql like latest post of WP but not displaying the latest
sanji41



 
Msg#: 4209719 posted 4:25 am on Oct 1, 2010 (gmt 0)

hi, i have a query that post articles in mysql. now i want to query the latest post but not the newest article.
e.i.
post id 1 <--inserted first
post id 2 <--inserted second
post id 3 <--inserted third
-------------------------
so when i run the query it should display the ff in DESC:
------------
post id 2
------------
post id 1
------------
'coz post id 3 is the newest and they are being viewed in the same page.
(so no need to post id 3 again.)
hope someone can help :D
P.S. (i need the query)

 

Anyango

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4209719 posted 9:30 am on Oct 1, 2010 (gmt 0)

field names to run query on ?

sanji41



 
Msg#: 4209719 posted 1:18 pm on Oct 1, 2010 (gmt 0)

thanks for the reply, fields will be title,content,date
its like a mimic of wordpress latest post except that it doesn't show the newly inserted post.. so it will like -1 for the id to be called..
hope you'll help me. thanks!

LifeinAsia

WebmasterWorld Administrator lifeinasia us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4209719 posted 3:41 pm on Oct 1, 2010 (gmt 0)

I would start with:
SELECT title,content,date
FROM TableName
WHERE id<>(SELECT MAX(id) FROM TableName)
ORDER BY id DESC

(This returns all the rows except the most recent- I don't know how to limit it to 1 result in MySQL.)

Anyango

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4209719 posted 6:21 pm on Oct 1, 2010 (gmt 0)

LIMIT 1 at the end of LifeInAsia's query

sanji41



 
Msg#: 4209719 posted 1:35 am on Oct 2, 2010 (gmt 0)

thanks man! i've tried that before but didn't work..your's did! thanks again! :D

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4209719 posted 5:00 pm on Oct 4, 2010 (gmt 0)

While that works, it could potentially break if there are any mods to the database in future updates . . . you should use the inherent WP functions. Additionally min and max are evaluation functions, if you go that route you can save overhead by just doing ".. order by id desc limit 1,1" (will pull one record starting with the second record, 0 is record one.)

Though they say in the documentation to include wp_head for external pages, you only need to include wp_load to make Wordpress functions accessible externally.


<?php
include ($_SERVER['DOCUMENT_ROOT'] . "/blog/wp-load.php");
//query_posts('showposts=1&category_name=Your Category if You Want');
query_posts('showposts=2&orderby=id&order=desc');
while (have_posts()) {
the_post();
$author_id = get_the_author_meta('ID');
$fn = get_the_author_meta('first_name');
$ln = get_the_author_meta('last_name');
$image = get_avatar( $author_id, 75, null, "$fn $ln");
$date = get_the_date();
$permalink= get_permalink();
$title= get_the_title();
echo "
<div class=\"blog-posts\">
<div class=\"blog-post-thumb\">$image</div>
<h2>$title</h2> <h4 class=\"dateline\"> $date</h4> <p>";
the_content();
echo "</div>';
}


The previous is **not exactly** correct as it doesn't skip the first one, but dig around a little, it will come to you.

Query Posts [codex.wordpress.org]

sanji41



 
Msg#: 4209719 posted 3:29 am on Oct 5, 2010 (gmt 0)

thanks man! although im using this on a small site, i think i will just update the code. And im not using WP, its my own cms in php. anyways, i'll try to know more about this. thanks for the code though. cheers!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved