ajax - Get post and all posts after it by ID?

Maybe this is super obvious and staring me in the face, but I'm stumped. Is there a way to pull and sort posts by I

Maybe this is super obvious and staring me in the face, but I'm stumped. Is there a way to pull and sort posts by ID? I'd like to grab an arbitrary post, and then x number of posts after it.

Specifically what I'm trying to do is a build a carousel of posts, and I'd like the first item on every individual post to be the current post, and then everything following it. I'd also like to be able to scroll the carousel backwards. I'm using the JSON API plugin to make AJAX calls to pull the posts.

Basically I just want to add a "WHERE post.id >= currentPost" to the query, but I can't figure out where to implement this. I tried using the posts_where hook but I always get zero results.

Maybe this is super obvious and staring me in the face, but I'm stumped. Is there a way to pull and sort posts by ID? I'd like to grab an arbitrary post, and then x number of posts after it.

Specifically what I'm trying to do is a build a carousel of posts, and I'd like the first item on every individual post to be the current post, and then everything following it. I'd also like to be able to scroll the carousel backwards. I'm using the JSON API plugin to make AJAX calls to pull the posts.

Basically I just want to add a "WHERE post.id >= currentPost" to the query, but I can't figure out where to implement this. I tried using the posts_where hook but I always get zero results.

Share Improve this question asked Jun 25, 2011 at 20:42 TheSeanzTheSeanz 111 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 1

I think you're on the right path with posts_where, something like this should work:

<?php
function my_filter_where($where){
    global $my_current_id;
    $where .= " AND ID >= ".$my_current_id;
    return $where;
}

// random number I chose for sake of example
$my_current_id = 30;

add_filter('posts_where', 'my_filter_where');

$my_posts = new WP_Query("posts_per_page=5&orderby=ID&order=ASC");

remove_filter('posts_where', 'my_filter_where');

Straightforward sorting by ID would be unreliable. Simply put IDs are not guaranteed to be sequential (even if they commonly are). One case would be import with custom IDs for example.

I suggest to look into get_adjacent_post() function. It only retrieves one post by default, but it has extensive hooks for its SQL query and LIMIT 1 seems easy to override by filtering get_{$adjacent}_post_sort hook.

However I am not sure how/if that can be implemented with JSON API specifics.

PS do you really need to do this asynchronously via Ajax? Maybe setting up JS variables with data in page source would work better?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745385696a4625429.html

相关推荐

  • ajax - Get post and all posts after it by ID?

    Maybe this is super obvious and staring me in the face, but I'm stumped. Is there a way to pull and sort posts by I

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信