Harman, don’t use get_posts()
when pagination is required
. You may also accomplish this task using WP_Query
.
Refer to this answer explained beautifully by Chip Bennett at
How to fix pagination for custom loops?
<?php $user_fav = get_user_favorites($user_id);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 4,
'post_type' => array('communityposts','post','video'),
'post__in' => $user_fav,
'post_status' => 'publish',
'suppress_filters' => true ,
'paged' => $paged
);
$loop = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $loop;
if( $loop->have_posts()):while($loop->have_posts()):$loop->the_post();
?>
<article class="post-items-latest">
<a href="https://wordpress.stackexchange.com/questions/194840/<?php echo get_permalink( $post->ID ); ?>" title="<?php the_title(); ?>">
<span class="image-wrapper">
<?php
$thumb = get_post_thumbnail_id($post->ID);
$img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big)
?>
<img src="<?php echo $img_url; ?>" alt="<?php the_title(); ?>" width="226" height="117">
</span>
<h1><?php the_title(); ?></h1>
</a>
<div class="post-footer">
<span class="user"><i class="fa fa-user"></i> <?php echo get_the_author_link(); ?></span>
<span class="data"><i class="fa fa-clock-o"></i> <?php wp_days_ago_v3(); ?></span>
<span class="likes"><i class="fa fa-eye"></i> <?php echo $meta_values = get_post_meta( get_the_ID(), 'cv_post_views_count', true ); ?> </span>
</div>
</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<div class="post-nav-container">
<?php previous_posts_link( __('→ Older Posts')); ?>
<?php next_posts_link( __('Newer Posts ← ',$loop->max_num_pages)); ?>
</div>
<?php
$wp_query = NULL;
$wp_query = $temp_query;
?>