問題描述

我有一個查詢顯示一些帖子。

$args = array(
    'post_type' =>'products'
    'posts_per_page'=> 12,
    'orderby'       => 'meta_value_num',
    'meta_key'  => '_price',
    'order' => 'asc',
);
$loop=new WP_Query($args);
while($loop->have_posts()) : $loop->the_post();
    the_content();
endwhile;

在這個循環中我得到的產品,但我需要在這個循環中首先出現一些產品。 12,13,14,34 這些是我的帖子 id,我需要首先出現在循環中。

我該如何首先顯示這個帖子,休息其他人? 。有沒有辦法做到這一點。如果 $ args 中的任何變量支持這種功能?

最佳解決方案

如果你需要:

  • 頁面查詢

  • 每頁保留 12 個帖子,而不是”sticking” 所需的帖子在所需的 12 個頂部

  • 只需要在第一頁顯示這些帖子

你可以嘗試以下幾點

$ids_args = [
    'post_type'      => 'products'
    'posts_per_page' => -1,
    'orderby'        => 'meta_value_num',
    'meta_key'       => '_price',
    'order'          => 'ASC',
    'fields'         => 'ids'
];
$all_posts_ids = get_posts( $ids_args );

// Make sure we have posts before continuing
if ( $all_posts_ids ) {
    // Set all our posts that should move to the front
    $move_to_front   = [12,13,14,34];
    // Add the array of posts to the front of our $all_posts_ids array
    $post_ids_merged = array_merge( $move_to_front, $all_posts_ids );
    // Make sure that we remove the ID's from their original positions
    $reordered_ids   = array_unique( $post_ids_merged );

    // Now we can run our normal query to display 12 posts per page
    $args = [
        'post_type'      => 'products'
        'posts_per_page' => 12,
        'post__in'       => $reordered_ids,
        'orderby'        => 'post__in',
        'order'          => 'ASC',
        'paged'          => get_query_var( 'paged', 1 ),
    ];
    $loop = new WP_Query( $args );
    while( $loop->have_posts() ) {
        $loop->the_post();
            the_content();
    }
    wp_reset_postdata();
}

如果你需要這些帖子

  • 在每個頁面上貼上 12 個帖子

  • 在分頁查詢中

您可以運行兩個查詢如下

// Set an array of id's to display in front
$move_to_front   = [12,13,14,34];
// Run the query to display the posts you need in front
$args_front = [
    'post_type'      => 'products'
    'posts_per_page' => count( $move_to_front ),
    'post__in'       => $move_to_front,
    'orderby'        => 'meta_value_num',
    'meta_key'       => '_price',
    'order'          => 'ASC',
];
$loop_front = new WP_Query( $args_front );
if( $loop_front->have_posts() ) {
    while( $loop_front->have_posts() ) {
        $loop_front->the_post();
            the_content();
    }
    wp_reset_postdata();
}

// Now we can run our major loop to display the other posts
$args = [
    'post_type'      => 'products'
    'posts_per_page' => 12,
    'post__not_in'   => $move_to_front,
    'orderby'        => 'meta_value_num',
    'meta_key'       => '_price',
    'order'          => 'ASC',
    'paged'          => get_query_var( 'paged', 1 ),
];
$loop = new WP_Query( $args );
if( $loop->have_posts() ) {
    while( $loop->have_posts() ) {
        $loop->the_post();
            the_content();
    }
    wp_reset_postdata();
}

如果您只需要前面那些帖子的一個頁面,那麼 @birgire 的解決方案就可以做到這一點

次佳解決方案

我們可以用兩個查詢:

首先,我們使用第一個查詢獲取我們需要的帖子,然後我們將其與我們的粘貼帖子 ID 進行合併,並將其提供到第二個查詢中,並使用 post__in 參數進行過濾和排序:

$args = [
    'post_type'           => 'products'
    'posts_per_page'      => 12,
    'orderby'             => 'meta_value_num',
    'meta_key'            => '_price',
    'order'               => 'asc',
    'ignore_sticky_posts' => true,
    'fields'              => 'ids',
];
$ids = get_posts( $args );
if( ! empty( $ids ) )
{
    $stickies = [12,13,14,34];
    $post__in = array_unique( array_merge(  $stickies, $ids ) );
    $args = [
        'post__in'            => $post__in,
        'orderby'             => 'post__in',
        'ignore_sticky_posts' => true,
    ];
    $loop = new WP_Query( $args );
    // ... etc
}

在那裏我們調整 $stickies 的自定義粘貼帖子。

參考文獻

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