[英]Two different custom query Wordpress loops with a single pagination
完全受困於此,因此希望有人可以對我的情況有所了解。
可以說我有10篇帖子-管理員用戶5篇,標准用戶5篇,我想每頁顯示3篇帖子(數字雖然高得多,但讓事情簡單點)
我希望按用戶角色對結果進行排序-在搜索結果頁面上執行搜索時,我希望看到的是這樣的:
第一頁
第二頁
第三頁
等等 ...
我一直想首先顯示來自Admin用戶的帖子,而且我還想以不同的方式顯示它們,這就是為什么我選擇使用兩個循環而不是按一個循環對帖子的順序進行排序的原因-此外,還會將過濾器應用於循環(按日期等排序),但是這樣做時,我仍然希望首先顯示管理信息,顯示最早的管理信息,並且僅在顯示所有管理信息后,再轉到標准信息...所以有兩個循環實際上,這是我唯一的選擇。
具有自定義查詢的兩個循環是我能獲得的最接近的結果:
第一頁
第二頁
因此,在我看來,我需要做的是在第一個循環完成時開始第二個循環……但我認為這是不可能的,如果我的問題有另一種解決方案,我將不知所措! :-)
這是我在失敗的榮耀中所做的最新嘗試-如果有人能指出正確的方向,我將非常感激!
<?php
$admin_users = get_users(
array(
'role' => 's2member_level1' ,
'fields' => 'ID'
)
);
$standard_users = get_users(
array(
'role' => 'subscriber' ,
'fields' => 'ID'
)
);
$admin_selector = array(
'author__in' => $admin_users,
'post_type' => 'post',
'posts_per_page' => 3,
'paged' => $paged
);
$standard_selector = array(
'author__in' => $standard_users,
'post_type' => 'post',
'posts_per_page' => 3,
'paged' => $paged
);
$admin_query = new WP_Query( $admin_selector );
$standard_query = new WP_Query( $standard_selector ); ?>
<?php if ( $admin_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $admin_query->have_posts() ) : $admin_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>">Recommended - <?php the_title(); ?></a></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php rewind_posts();?>
<?php wp_reset_query();?>
<?php if ( $standard_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $standard_query->have_posts() ) : $standard_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<div class="pagination-links">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</div>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
$ admin_users和$ standard_users返回具有此級別的用戶的ID。
現在,創建一個將兩個數組合並為一個的新變量:
$id_users = array_merge($admin_users,$standard_users);
使用兩個查詢不是更好的解決方案,僅創建一個WP_Query像這樣:
$users_query = new WP_Query( array(
'author' => implode( ',', $id_users ),
'post_type' => 'post',
'posts_per_page' => 3,
'paged' => get_query_var('paged'),
'orderby' => 'author',
'order' => 'ASC') );
並且應該可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.