I am trying to display post numbers (for ranking purposes) on my feed, this was accomplished by calling the function that contains my wordpress loop and returning an incremented value. However, by doing so, it appears that the loop stops finding new posts and instead repeats the content (only repeating content below this function call)such as the article title (the featured image is different because this was called before the function). How do I go about fixing this?

Here's what it looks like:

enter image description here

Loop found in plugin.php

function ori_load_more_func() {

//removed irrelevant code

  // Query the posts by the specified IDs, and sort by like count
    $args = array(
      'post_type'   => array( 'post'),
      'post__in'      => $most_popular_posts,
      'meta_key'    => '_liked',
      'orderby' => array( 'meta_value_num' => 'DESC', 'date' => 'DESC' ),
      'post_status' => 'publish',
      'paged'       => $offset,
      'posts_per_page'  => $posts_per_page,
      'post__not_in' => array($current_post_id) //skip current displayed post


    global $ranking;

  $posts_query = new WP_Query( $args );

  if ($posts_query->have_posts()) {

          $result['have_posts'] = true;
         // echo '<div class="load_more_single_post_container">';

          while ( $posts_query->have_posts() ) : $posts_query->the_post();

            return $ranking += 1;

            echo get_template_part( 'template-parts/content', get_post_format() );


//      $ranking = $ranking + 1;
        //echo '</div>';
        $data = ob_get_clean();
        wp_send_json_success( $data );



Where the function is called in content.php

<div class="rank-number">
  <?php echo ori_load_more_func($ranking); ?>

1 Answer 1


first of all, you could delete the return $ranking += 1; and replace by $ranking += 1;.

We don't know what happend in the template-parts/content, but don't forget to add a little <?php wp_reset_postdata(); ?> just after your endwhile;.

  • Hi @Paco, removing the return breaks my site. Don't I need that since I'm calling a function value? Added the wp_reset_postdata afterwards with no avail either Feb 10, 2017 at 9:07
  • here's the live site by the way: uncvrd.co Feb 10, 2017 at 9:09
  • I would like to know if the trouble is in the query or after. Can you try to keep in your query the stric minimum parameters ? Just try with ` $args = array( 'post_type' => array( 'post') );` and test the result width while ( $posts_query->have_posts() ) : $posts_query->the_post(); the_title(); logically, you retrieve all post title. If it's ok, you can add parameters in your query. You probably find the problem. Feb 10, 2017 at 14:40
  • yes, the loop runs perfectly and displays all posts. The only time it stops working is when I throw that function call into my other PHP file. It's as if, once the value is "returned" it stops loading new material until the iteration finishes. I mean...since this loop is in a function, and I'm calling a return value, wouldn't the function stop running on that iteration after returning the value? Which is why no new data is loaded? Feb 10, 2017 at 15:14
  • So try to replace $data = ob_get_clean(); by return ob_get_clean(); and remove the return $ranking += 1; and write $ranking += 1;. For avoid problems, you can in your function, write function ori_load_more_func( $ranking = 0 ) { Feb 10, 2017 at 16:12

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.