Advertisement
Twansparant

Better Related Posts Error Workaround

Jan 27th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php $scores = the_related_get_scores(get_the_ID(), array(
  2.     'usept' => array(
  3.         'post'  => true
  4.     ),
  5.     'usetax'        => array(
  6.         'post_tag'  => true
  7.     ),
  8. ));
  9. if ($scores){ // Prevent array_keys() warnings if $scores is not an array
  10.     $posts = array_slice(array_keys( $scores ), 0, 5 ); // keep only the the five best results
  11.     $args = array(
  12.         'post__in'          => $posts,
  13.         'posts_per_page'    => 5,
  14.         'caller_get_posts'  => 1,
  15.     );
  16.     $my_query = new WP_Query($args);
  17.     function orderby( $a, $b ) {
  18.         global $posts;
  19.         $apos   = array_search( $a->ID, $posts );
  20.         $bpos   = array_search( $b->ID, $posts );
  21.         return ( $apos < $bpos ) ? -1 : 1;
  22.     }
  23.     usort( $my_query->posts, "orderby" );
  24.     if ( $my_query->have_posts()) { ?>
  25.         <ul class="posts">
  26.         <?php while ( $my_query->have_posts()) : $my_query->the_post(); ?>
  27.            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  28.         <?php endwhile; ?>
  29.         <?php wp_reset_query(); ?>
  30.         </ul>
  31.     <?php } else { ?>
  32.         <p>No related posts found</p>
  33.     <?php } ?>
  34. <?php } else {
  35.     the_related(get_the_ID(),
  36.         array(
  37.             'usept'         => array(
  38.                 'post'  => true
  39.             ),
  40.             'usetax'        => array(
  41.                 'category'  => false,
  42.                 'post_tag'  => true
  43.             ),
  44.             'maxresults'    => 5,
  45.             'storage_id'    => 'better-related-post-',
  46.             'relatednone'   => '<p>No related posts found</p>'
  47.         )
  48.     );
  49. } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement