frostiecuk Posted November 29, 2012 Share Posted November 29, 2012 (edited) Hi all I'm having fun and games (well, not really fun) trying to get a SELECT statement to work on a wordpress website and wondered if anyone out there could help. Basically I am trying to retrieve 10 posts that have an expiry date of today. Selecting 10 posts is fine, the problem start when I try to filter it based on date. SELECT * FROM wp_posts WHERE post_type = "listing" LIMIT 10 However the expiry date is held in a different table; Table: wp_postmeta Field: listing_expiry_date Date: is stored in a column called meta_value (in mm-dd-yyyy format) I suspect the link between wp_posts and wp_postmeta takes place using post_id. Although I know what it is that needs to happen, I'm at a loss as to how to code it correctly; SELECT * FROM wp_posts WHERE post_type = "listing" WHERE meta_value IN listing_expiry_date in wp_postmeta = TODAY LIMIT 10 Can anyone please advise how to go about this? Many thanks Edited November 29, 2012 by frostiecuk Quote Link to comment https://forums.phpfreaks.com/topic/271336-select-data-from-a-different-table/ Share on other sites More sharing options...
Barand Posted November 29, 2012 Share Posted November 29, 2012 (edited) Use a JOIN SELECT * FROM wp_posts INNER JOIN wp_postmeta USING (post_id) WHERE post_type = 'listing' AND listing_expiry_date = '11-29-2012' LIMIT 10 edit PS. your date format is pretty useless. You should use DATE type fields, format yyyy-mm-dd, which allows for comparisons and sorting and can be used by the inbuilt shedload of mysql datetime functions Edited November 29, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/271336-select-data-from-a-different-table/#findComment-1396128 Share on other sites More sharing options...
frostiecuk Posted November 29, 2012 Author Share Posted November 29, 2012 Hi Barand Firstly many thanks. I got an error "Unknown column 'post_id' in 'from clause' but based your suggestion have concocted the following; SELECT * FROM wp_posts INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE post_type = 'listing' AND post_status = 'publish' AND meta_value = '11-29-2012' LIMIT 10 This works a treat, however now I need the date to be dynamic (i.e. not an actual date, but @Today). I've tried GETDATE TODAY AND CURDATE but nothing seems to return the correct results. Any ideas on this last tweak? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/271336-select-data-from-a-different-table/#findComment-1396133 Share on other sites More sharing options...
Barand Posted November 29, 2012 Share Posted November 29, 2012 you'll need to reformat CURDATE() DATE_FORMAT(CURDATE(), '%m-%d-%Y') Quote Link to comment https://forums.phpfreaks.com/topic/271336-select-data-from-a-different-table/#findComment-1396134 Share on other sites More sharing options...
frostiecuk Posted November 29, 2012 Author Share Posted November 29, 2012 I cracked it and was coming back here to let you know. I've used the following; DATE_FORMAT(NOW(),'%m-%d-%Y') Is there any reason why I should use NOW over CURDATE or the other way round? Quote Link to comment https://forums.phpfreaks.com/topic/271336-select-data-from-a-different-table/#findComment-1396135 Share on other sites More sharing options...
Barand Posted November 29, 2012 Share Posted November 29, 2012 NOW() is the current date and time CURDATE() is the current date. If I'm only concerned with the date, I use CURDATE() Quote Link to comment https://forums.phpfreaks.com/topic/271336-select-data-from-a-different-table/#findComment-1396136 Share on other sites More sharing options...
frostiecuk Posted November 29, 2012 Author Share Posted November 29, 2012 Thanks once again. Changed it to CURDATE and it works an absolute treat. Quote Link to comment https://forums.phpfreaks.com/topic/271336-select-data-from-a-different-table/#findComment-1396144 Share on other sites More sharing options...
fenway Posted November 30, 2012 Share Posted November 30, 2012 Of course, you should really be storing dates as DATEs. Quote Link to comment https://forums.phpfreaks.com/topic/271336-select-data-from-a-different-table/#findComment-1396372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.