Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Post Duplication (View Only) #61

Merged
merged 2 commits into from
Jul 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/blocks/homepage-articles/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
* @return string Returns the post content with latest posts added.
*/
function newspack_blocks_render_block_homepage_articles( $attributes ) {
global $newspack_blocks_post_id;
if ( ! $newspack_blocks_post_id ) {
$newspack_blocks_post_id = array();
}
$author = isset( $attributes['author'] ) ? $attributes['author'] : '';
$categories = isset( $attributes['categories'] ) ? $attributes['categories'] : '';
$posts_to_show = intval( $attributes['postsToShow'] );
$args = array(
'posts_per_page' => $attributes['postsToShow'],
'posts_per_page' => $posts_to_show + count( $newspack_blocks_post_id ),
'post_status' => 'publish',
'suppress_filters' => false,
'cat' => $categories,
Expand Down Expand Up @@ -45,6 +50,7 @@ function newspack_blocks_render_block_homepage_articles( $attributes ) {
if ( isset( $attributes['className'] ) ) {
$classes .= ' ' . $attributes['className'];
}
$post_counter = 0;

ob_start();

Expand All @@ -60,6 +66,11 @@ function newspack_blocks_render_block_homepage_articles( $attributes ) {
endif;
while ( $article_query->have_posts() ) :
$article_query->the_post();
if ( isset( $newspack_blocks_post_id[ get_the_ID() ] ) || $post_counter >= $posts_to_show ) {
continue;
}
$newspack_blocks_post_id[ get_the_ID() ] = true;
$post_counter++;
?>
<article <?php echo has_post_thumbnail() ? 'class="post-has-image"' : ''; ?>>
<?php if ( has_post_thumbnail() && $attributes['showImage'] ) : ?>
Expand Down