Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save acerus/b7f4063e88343d47f5a593889af3b488 to your computer and use it in GitHub Desktop.
Save acerus/b7f4063e88343d47f5a593889af3b488 to your computer and use it in GitHub Desktop.
Get posts from all blogs in multisite WordPress with pagination
<?php
//Querying all posts in the argument
$args = [
'post_type' => 'post',
'order' => 'DESC',
'category_name' => 'news',
'posts_per_page' => 999,
'post_status' => 'publish'
];
//Setting up a query
$query = new WP_Query( $args );
//Listing all site ID's
$sites = [ 1, 2 ];
//Setting up empty array list
$all_posts = [];
// Looping all the sites and compiling posts in the array $all_posts
foreach ( $sites as $site ) {
switch_to_blog( $site );
$posts = get_posts( $args );
foreach ( $posts as $post ) {
// Better get the post's permalink, since it's site-dependent
$post->permalink = get_permalink( $post->ID );
$post->thumbnail = get_the_post_thumbnail_url( $post->ID, 'thumbnail' );
$all_posts[] = $post;
}
//restoring to the default blog
restore_current_blog();
}
//sorting posts using the sort function
$all_posts = wp_list_sort( $all_posts, 'post_date', 'DESC' );
//manually setting the number of page articles
$nb_elem_per_page = 5;
//manually setting the $paged item
$page = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
//calculating the number of paged elements
$posts_per_page = intval( ceil( count( $all_posts ) / $nb_elem_per_page ) );
if ( $page === 1 ) {
$company_news = array_slice( $all_posts, 0, $nb_elem_per_page );
} else {
$company_news = array_slice( $all_posts, ( $page - 1 ) * $nb_elem_per_page, $nb_elem_per_page );
}
?>
<section class="press-section">
<div class="quotes_wrapper">
<?php
//displaying all posts with pagination to follow
foreach ( $company_news as $quote ) {
setup_postdata( $quote );
?>
<article class="single_quote flx mob-no-flex pb-40">
<div class="quote_meta w-100">
<?php if ( $quote->thumbnail ): ?>
<a class="no_underline quote_thumb" href="<?= $quote->permalink; ?>">
<img class="quote_thumb_img lazyload l-border b-radius-15 trans-all-300" data-src="<?= $quote->thumbnail ?>" data-placeholder-background="#FFFFFF" alt="<?= $quote->post_title; ?>" width="248" height="156">
</a>
<?php endif; ?>
</div>
<div class="quote_details">
<h2 class="quote_headline">
<a class="no_underline blueberry" href="<?= $quote->permalink; ?>"><?= $quote->post_title; ?></a>
</h2>
<?php if ( $quote->post_excerpt ) : ?>
<div class="quote_text fw-300 mt-20 mb-15 pl-0 no-before">
<?= $quote->post_excerpt; ?>
</div>
<?php endif; ?>
<p class="date grey mt-0 fs-16"><?= date( 'M j, Y', strtotime( $quote->post_date ) ) ?></p>
</div>
</article>
<?php wp_reset_query();
}
if ( $args['total'] && $args['page'] ) { ?>
<div class="quotes_pagination flx centered h-center v-center fs-16">
<?= wp_kses_post(
paginate_links(
[
'current' => $args['page'],
'total' => $args['total'],
'show_all' => true,
]
)
); ?>
</div>
<?php } ?>
</div>
</section>
@odera89
Copy link

odera89 commented May 31, 2022

Hey,

$args['total'] and $args['page'] variables has null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment