0

I have created a page where user can see his favourite post which has been bookmarked by him

get_user_favorites($user_id);

This function is responsible for returning me all the favourite posts bookmarked by user in an array.

I am using this approach to get the data from the database and make it work inside my Custom Html.

    <?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
      );
      $posts_array = get_posts( $args );
       foreach ($posts_array as $p) :

    ?>

<article class="post-items-latest">
  <a href="<?php echo get_permalink( $p->ID ); ?>" title="<?php echo $p->post_title; ?>">
    <span class="image-wrapper">

      <?php
        $thumb = get_post_thumbnail_id($p->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 $p->post_title; ?>" width="226" height="117">

    </span>
    <h1><?php echo $p->post_title; ?></h1>
  </a>
  <div class="post-footer">
    <span class="user"><i class="fa fa-user"></i>&nbsp;<?php echo get_the_author_link(); ?></span>
    <span class="data"><i class="fa fa-clock-o"></i>&nbsp;<?php wp_days_ago_v3(); ?></span>
    <span class="likes"><i class="fa fa-eye"></i>&nbsp;<?php echo $meta_values = get_post_meta( get_the_ID(), 'cv_post_views_count', true ); ?> </span>
  </div>
</article>

<?php endforeach; ?>


<div class="post-nav-container">
  <?php previous_posts_link( __('&rarr; Older Posts','circoviral')); ?>
  <?php next_posts_link( __('&larr; Older Posts','circoviral')); ?>
</div>

Using this approach i am able to get the Favourited post by an User but i am not able to make the next_posts_link(); and previous_posts_link(); work inside my template

Is there anything i am doing wrong ?

1
  • Don't use get_posts(), rather use WP_Query.
    – terminator
    Commented Jul 17, 2015 at 20:45

2 Answers 2

1

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="<?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>&nbsp;<?php echo get_the_author_link(); ?></span>
    <span class="data"><i class="fa fa-clock-o"></i>&nbsp;<?php wp_days_ago_v3(); ?></span>
    <span class="likes"><i class="fa fa-eye"></i>&nbsp;<?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( __('&rarr; Older Posts')); ?>
  <?php next_posts_link( __('Newer Posts &larr; ',$loop->max_num_pages)); ?>
</div>

<?php 
$wp_query = NULL;
$wp_query = $temp_query;
?>
7
  • No luck ,still Not getting the pagination links Commented Jul 18, 2015 at 6:21
  • Ur navigation links not showing up or they are not working
    – terminator
    Commented Jul 18, 2015 at 8:09
  • Links are not showing up Commented Jul 18, 2015 at 8:11
  • @HarmanPreet Please try the updated anwser, it may work
    – terminator
    Commented Jul 18, 2015 at 21:37
  • 1
    @HarmanPreet as this answer solved your issue, you should, in good sportmanship and in appreciation, accept this answer by clicking on the checkmark next to this answer. Thank you :-) Commented Jul 20, 2015 at 4:31
0

My approach was also correct but the only problem was we were missing is that resetting the query before the post navigation links.

Here is the updated answer.

  <?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,
      'order_by'         => 'date',
      'order'            => 'ASC' 
    );
    $wp_query = new WP_Query( $args );

    if( $wp_query->have_posts()):while($wp_query->have_posts()):$wp_query->the_post();

  ?>

  <article class="post-items-latest">
    <a href="<?php the_permalink(); ?>" 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)
            $image = aq_resize( $img_url, 226, 117,true,true,true); //resize & crop the image
          ?>

          <?php if(!empty($image)): ?>
            <img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" width="226" height="117">
          <?php else: ?>

            <?php if (has_post_thumbnail()): ?>

          <?php the_post_thumbnail( 'large'); ?>

            <?php endif; ?>
          <?php endif; ?>

      </span>
      <h1><?php the_title(); ?></h1>
    </a>
    <div class="post-footer">
      <span class="user"><i class="fa fa-user"></i>&nbsp;<?php echo get_the_author_link(); ?></span>
      <span class="data"><i class="fa fa-clock-o"></i>&nbsp;<?php wp_days_ago_v3(); ?></span>
      <span class="likes"><i class="fa fa-eye"></i>&nbsp;<?php echo $meta_values = get_post_meta( get_the_ID(), 'cv_post_views_count', true ); ?> </span>
    </div>
  </article>

  <?php 
    endwhile;
    endif;
  ?>

  <div class="post-nav-container">
    <?php previous_posts_link( __('&rarr; Older Posts')); ?>
    <?php next_posts_link( __('Newer Posts &larr; ')); ?>
  </div>
  <?php wp_reset_query(); ?>
1
  • @terminator changed the complete code , so i just posted his previous code that helped me to solve the problem , Only Problem was resetting the query before the navigation links. I didn't tested his new answer so i am not sure it will gonna work or not ! Commented Jul 20, 2015 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.