MySQL query error driving me nuts
-
I’m trying to run a MySQL query via WordPress, to bring back a list of posts that I want to delete because they have no “like” votes. The query works perfectly in phpMyAdmin but gives a syntax error when I run it through WP… and I see absolutely no reason why it would do this.
Here’s the query code, which checks for posts over 30 days old which have no corresponding “like” entry in wti_like_post (whether positive or negative):
$novotesquery = "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_date < DATE_SUB(NOW(), INTERVAL 30 DAY) AND $wpdb->posts.ID NOT IN (SELECT DISTINCT post_id FROM $wpdb->wti_like_post)" ; $result = $wpdb->get_results($novotesquery); foreach ($result as $post) { setup_postdata($post); $postid = $post->ID; wp_delete_post($postid); }
The syntax error says there’s a problem on the last line (the SELECT in parentheses): “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘)’ at line 5”.
If anyone can tell me why the SQL query will run on the server and not in WP, I’d appreciate it.
Thanks in advance.
- The topic ‘MySQL query error driving me nuts’ is closed to new replies.