0

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
    );

  ob_start();

    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() );



         endwhile;

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

  }


}

Where the function is called in content.php

<div class="rank-number">
  <?php echo ori_load_more_func($ranking); ?>
</div>
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 – Jordan Lewallen Feb 10 '17 at 9:07
  • here's the live site by the way: uncvrd.co – Jordan Lewallen Feb 10 '17 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. – Paco Ciccarelli Feb 10 '17 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? – Jordan Lewallen Feb 10 '17 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 ) { – Paco Ciccarelli Feb 10 '17 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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