I have tried several things but I can't get the MySQL query in my php code to work. Note that the query DOES WORK when I hard-code the variable $lastmsg which is an integer.
The code works in two instances:
- When the page loads, the query runs, ignoring
$pagination
because$lastmsg
is not set. - A button sends a value for lastmsg to this page, which runs the code again, this time looking at $pagination because lastmsg is set.
Again, the query DOES WORK. The proof being that it runs perfectly fine in the first instance and returns properly when the variables are hard coded. Firebugs reports that lastmsg is being sent by post.
The problem is that the query isn't receiving the value for $lastmsg (I get no values returned). I know this is because $lastmsg is sitting inside $pagination.
P.S. I am aware it needs injection protection... I'm just trying to get this to work:
if(isset($_POST['lastmsg']) &&is_numeric($_POST['lastmsg'])){
$lastmsg = $_POST['lastmsg'];
$pagination = 'AND $wpdb->posts.ID < ".$lastmsg."';
} else {
$pagination = '';
}
$pageposts = $wpdb->get_results($wpdb->prepare("
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()+INTERVAL 1 DAY
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN(3)
".$pagination."
ORDER BY $wpdb->posts.post_date DESC
LIMIT 10
"), OBJECT);
The following DOES NOT work
$pagination = 'AND $wpdb->posts.ID < ';
$pageposts = $wpdb->get_results($wpdb->prepare("
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()+INTERVAL 1 DAY
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN(3)
".$pagination."
".$lastmsg."
ORDER BY $wpdb->posts.post_date DESC
LIMIT 10
"), OBJECT);
Any solutions?