Guest User

Untitled

a guest
Oct 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. <?php
  2. /**
  3. * Return posts with maximum age of one year.
  4. *
  5. * Usage:
  6. * add_filter( 'posts_where', 'wp_query_filter_max_one_year_old' );
  7. * $my_query = new WP_Query( array( ... ) );
  8. * remove_filter( 'posts_where', 'wp_query_filter_max_one_year_old' );
  9. *
  10. * @param string $where
  11. * @return string
  12. */
  13. function wp_query_filter_max_one_year_old( $where = '' ) {
  14. $where .= " AND post_date > '" . date( 'Y-m-d', strtotime( '-356 days' ) ) . "'";
  15. return $where;
  16. }
  17.  
  18. /**
  19. * Find similar posts based on tags.
  20. *
  21. * @param int $post_id
  22. * @param integer $limit maximum number of posts
  23. * @param integer $cache_duration time to cache in seconds. default: 1 week
  24. * @return array list of post_ids
  25. */
  26. function get_similar_post_ids_by_tags( $post_id = NULL, $limit = 3, $cache_duration = 604800 ) {
  27.  
  28. if ( ! $post_id )
  29. $post_id = get_the_ID();
  30.  
  31. $cache_key = "inps_similar_" . $post_id;
  32.  
  33. if ( false === ( $scores = get_transient( $cache_key ) ) ) {
  34.  
  35. $tags = wp_get_post_tags( $post_id );
  36.  
  37. // fetch all last years posts with at least one matching tag
  38. $tag_ids = array_map( function( $t ) { return $t->term_id; }, $tags );
  39. add_filter( 'posts_where', 'wp_query_filter_max_one_year_old' );
  40. $args = array(
  41. 'tag__in' => $tag_ids,
  42. 'post__not_in' => array( $post_id ),
  43. 'posts_per_page' => -1
  44. );
  45. $query_similar_posts = new WP_Query( $args );
  46. remove_filter( 'posts_where', 'wp_query_filter_max_one_year_old' );
  47.  
  48. $scores = array();
  49.  
  50. // count identical tags for each post
  51. while ( $query_similar_posts->have_posts() ) {
  52. $p = $query_similar_posts->next_post();
  53.  
  54. $identical_tags = 0;
  55. $this_tags = wp_get_post_tags( $p->ID );
  56. foreach ( $this_tags as $t ) {
  57. if ( in_array( $t->term_id, $tag_ids ) ) {
  58. $identical_tags++;
  59. }
  60. }
  61.  
  62. if ( $identical_tags > 0 ) {
  63. $scores[] = array( 'post_id' => $p->ID, 'score' => $identical_tags );
  64. }
  65. }
  66.  
  67. // sort by score
  68. usort( $scores, function ( $a, $b ) {
  69. if ( $a[ 'score' ] == $b[ 'score' ] ) {
  70. return 0;
  71. }
  72. return ( $a[ 'score' ] > $b[ 'score' ] ) ? -1 : 1;
  73. } );
  74.  
  75. // take first $limit only
  76. $scores = array_slice( $scores, 0, $limit );
  77.  
  78. // return without scores
  79. $scores = array_map( function( $p ){ return $p[ 'post_id' ]; }, $scores );
  80.  
  81. set_transient( $cache_key, $scores, $cache_duration );
  82. }
  83.  
  84. return $scores;
  85. }
  86.  
  87. ?>
  88. <!-- Example for Theme file -->
  89. <?php $similar_posts = get_similar_post_ids_by_tags(); ?>
  90. <strong><?php __( 'Related Posts' ) ?></strong>
  91. <ul>
  92. <?php foreach ( $similar_posts as $similar_post_id ): ?>
  93. <li>
  94. <a href="<?php echo get_permalink( $similar_post_id ); ?>"><?php echo get_the_title( $similar_post_id ); ?></a>
  95. </li>
  96. <?php endforeach; ?>
  97. </ul>
Add Comment
Please, Sign In to add comment