简体   繁体   中英

Two different custom query Wordpress loops with a single pagination

Completely stumped on this one, so here's hoping someone can shed some light on my situation.

Lets say I have 10 posts - 5 from Admin users and 5 from standard users and I want to show 3 posts per page (the numbers are much higher but lets keep things simple)

I'd want my results to be sorted by the users role - when preforming a search on the search results page(s) I want to see is something like this:

Page one

  • Admin post 1
  • Admin post 2
  • Admin post 3

Page two

  • Admin post 4
  • Admin post 5
  • Standard post 1

Page three

  • Standard post 2
  • Standard post 3
  • Standard post 4

And so on ...

I always want to display posts from Admin users first and I also want to display them in a different way which is why I've chosen to use two loops rather than sorting the order of posts in one loop - also there will be filters applied to the loops (sort by date etc) but when doing so I still want the admin posts to be displayed first, show the earliest admin post and only when all of them have been displayed then move on to the standard posts ... so two loops is really my only option in my instance.

Having two loops with custom queries the closest I've been able to get is this:

Page one

  • Admin post 1
  • Admin post 2
  • Admin post 3
  • Standard post 1
  • Standard post 2
  • Standard post 3

Page two

  • Admin post 4
  • Admin post 5
  • Standard post 4
  • Standard post 5

So in my mind what I need to do is start the second loop when the first one is complete ... but I don't think that's even possible, if there's another solution to my problem I'm all ears! :-)

Here's my latest attempted in all in failing glory - if anyone can point me in the right direction I'd be so grateful!

<?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 and $standard_users return id's of users that have this level.

Now, create a new variables that merge two arrays into one:

$id_users = array_merge($admin_users,$standard_users);

It's not a better solution used two query, create only one WP_Query like this:

$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') ); 

And should be work correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM