按帖子ID获取上一篇和下一篇帖子

时间:2012-06-14 作者:Zach Lysobey

我如何才能从循环外部获取上一篇和下一篇帖子?除了自定义选择查询(使用get_postsWP_Query) 将内容进一步分解到页面中。

我目前有一个递归函数,希望它能找到满足特定条件的第一个“前一个”帖子。我从中获取第一个要测试的值get_previous_post() 但我不知道如何获得previous_previous_post().

add_action( \'wp_ajax_nopriv_myajax-submit\', \'ajax_project_load\' );
add_action( \'wp_ajax_myajax-submit\', \'ajax_project_load\' );

function ajax_project_load() {
    $the_slug = $_POST[\'slug\'];
    $args=array(
      \'name\' => $the_slug,
      \'post_type\' => \'projects\',
      \'post_status\' => \'publish\',
      \'showposts\' => 1,
      \'ignore_sticky_posts\' => 1
    );
    $my_posts = get_posts($args);
    if( $my_posts ) :

        global $post;
        $post = $my_posts[0]; 
        $postCapabilityFilter = $_POST[\'capFilter\']==\'undefined\' ? $_POST[\'capFilter\'] : substr($_POST[\'capFilter\'], 1);

        $response = json_encode( "Success" );
        header( "Content-Type: application/json" );     


$next_post = get_previous_post();
function filtered_next_post($next_post){
    if($next_post){
        $next_slug = $next_post->post_name;
        $next_ID = $next_post->ID;
        global $wpdb;
        global $postCapabilityFilter;
        $querystr = "
        SELECT $wpdb->postmeta.*
        FROM $wpdb->posts, $wpdb->postmeta
        WHERE $wpdb->posts.post_type = \'projects\' 
        AND $wpdb->posts.post_status = \'publish\' 
        AND $wpdb->posts.ID = $next_ID
        AND $wpdb->posts.ID = $wpdb->postmeta.post_id
        AND $wpdb->postmeta.meta_key = \'_slideshow_content\'
        ORDER BY $wpdb->posts.post_name DESC
        ";
        $nextProjQuery = $wpdb->get_results($querystr, OBJECT);
        foreach($nextProjQuery as $nextProj):
            $nextProjMetaArr = unserialize($nextProj->meta_value);
            foreach ($nextProjMetaArr[\'slides\'] as $npSlideArr){
                echo \'and..\';
                if ( !in_array( $postCapabilityFilter, $npSlideArr[\'slideCap\'] ) ){
                    echo \'not in it..\';
                    //$next_post = get_previous_previous_post();
                    //filtered_next_post($next_post);
                }
            }
        endforeach;                        
        return $next_slug;
    }
}
$next_slug = filtered_next_post($next_post);

0 个回复
结束

相关推荐

WordPress:使用Category Slug通过MySQL查询获取类别ID

基本上,我有一个存储了类别slug的cookie,我想使用这些数据查询数据库并返回类别的ID。我是mysql新手,这似乎是一个高级查询,可能需要使用连接,如果您有任何帮助或指导,我将不胜感激。非常感谢。参考号:http://codex.wordpress.org/File:WP3.0-ERD.png