get_posts for an infinite scroll plugin?
-
I have a query that loads 5 newer posts upon being triggered. Then the newest post on the page is registered so that when the button is again clicked, 5 newer-newer posts are loaded. (It’s an infinite scroll query that lets a user load both newer and older posts). My query is in a PHP file called by AJAX, and it looks like this:
switch ($postsAge) { case "olderPosts": $query = " SELECT ID, post_content, post_title, post_date FROM DBPREFIX_posts WHERE ID < $referenceID AND post_status = 'publish' AND post_type = 'post' ORDER BY ID DESC LIMIT 5; "; break; case "newerPosts": $query = " SELECT ID, post_content, post_title, post_date FROM DBPREFIX_posts WHERE ID > $referenceID AND post_status = 'publish' AND post_type = 'post' ORDER BY ID DESC LIMIT 5; "; break; }This query grabs both new and old posts depending on which button is clicked. $postsAge tracks this decision by getting the ID of the button clicked by the user and passes it to the query.
$referenceID is used to keep track of the newest/oldest post currently displayed to the user, and it’s passed to this query when the user triggers for more posts to load so that only posts newer than the newest post are displayed.
This works fine (although it may have security holes), but I would rather use a WordPress function like get_posts( $args ) to perform this query. I tried but wasn’t sure how to pass a variable like $referenceID. Any thoughts? How would I keep the same functionality while using get_posts (or another WP function)?
Avenues I’m exploring:
• The ‘offset’ argument.
• The ‘post_parent’ argument.
• The ‘posts_per_page’ argument.Cheers!
The topic ‘get_posts for an infinite scroll plugin?’ is closed to new replies.