問題描述

如何從迴圈之外獲取上一個和下一個帖子?除了自定義 SELECT 查詢 (使用 get_postsWP_Query) 之外,我嘗試的一切都將在頁面上進一步打破事情。

我目前有一個遞迴函式,希望能找到滿足一定條件的第一個”previous” 檔案。我抓住第一個值從 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);

最佳解決方案

看看 get_previous_post()get_next_post(),你會看到他們都使用 get_adjacent_post()來查詢上一個或下一個帖子。

假設您想要根據當前帖子的 ID 獲取上一個帖子的 ID 。這是你會做的:

function get_previous_post_id( $post_id ) {
    // Get a global post reference since get_adjacent_post() references it
    global $post;

    // Store the existing post object for later so we don't lose it
    $oldGlobal = $post;

    // Get the post object for the specified post and place it in the global variable
    $post = get_post( $post_id );

    // Get the post object for the previous post
    $previous_post = get_previous_post();

    // Reset our global object
    $post = $oldGlobal;

    if ( '' == $previous_post ) {
        return 0;
    }

    return $previous_post->ID;
}

你可以做一個類似的事情來獲取下一個帖子的 ID … 你可以遞迴地執行,如果你需要獲得上一個上一篇文章:

$two_posts_ago = get_previous_post_id( get_previous_post_id( $post->ID ) );

長文慎入

基本上,get_previous_post()get_next_post()都參考全域性 $post 物件進行選擇。在呼叫這兩個函式之前,您需要設定此物件,否則它們將不會知道要用作下一個/前一個參考的帖子。

上面的包裝功能只是基於 passed-in ID 設定了全域性 $post 。你可以讓它返回上一個帖子的整個物件,而不是 ID,這完全取決於你。

次佳解決方案

在這裡,您可以使用自定義 sql 查詢& 與過濾器 get_{$adjacent}_post_where 其中預設相鄰是以前。也取決於 $current_post_date& 比較運運算元 $op

function bm_get_adjacent_post( $post_id, $author_id, $previous = 1 ) {
global $wpdb;

if ( ( ! $post = get_post( $post_id ) )  )
    return null;

$current_post_date = $post->post_date;

$adjacent = $previous ? 'previous' : 'next';
$op = $previous ? '<' : '>';
$order = $previous ? 'DESC' : 'ASC';

$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE  p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' AND p.post_author = %d", $current_post_date, 'projects' , $author_id ), '', '' );
$sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );

$query = "SELECT p.ID FROM $wpdb->posts AS p $where $sort";
//echo $query;
$result = $wpdb->get_var( $query );
if ( null === $result )
    $result = '';
if ( $result )
    $result = get_post( $result );

return $result;
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。