dimanche 12 juin 2016

Converting longtext to datetime for use in wordpress (MySQL database)

I am working on a wordpress website and I need to get some sort of calendar working, where I get the upcoming event to show in the sidebar. To do this, I added a meta field (date) to the custom post type. What I would like to do, is fetch the upcoming event through sql (MySQL).

I know you can compare datetimes to certain functions (such as CUR_DATE() and NOW()), but the problem is that, due to my data being stored as a metafield, it has the longtext type and not the datetime type, making it impossible to compare and select my data to the mentioned functionalities. I tried using CONVERT, but this throws a syntax error. Any idea on how I can solve this?

Below you can find the sql that is throwing the syntax error. If I leave out the statement with CONVERT in it, I get results. My dates are formatted as d-m-Y btw.

SELECT p.ID FROM tt_posts p
INNER JOIN tt_postmeta pm
ON p.ID = pm.post_id

WHERE 
    p.post_type = "activity" AND
    p.post_status = "publish" AND
    pm.meta_key = "date" AND
    CONVERT(DATETIME, pm.meta_value) >= NOW()

ORDER BY pm.meta_value ASC

Aucun commentaire:

Enregistrer un commentaire