Advertisement
phpface

Untitled

Nov 24th, 2017
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. if( ! function_exists( 'videtube_filter_related_widget_args' ) ){
  2.     /**
  3.      *
  4.      * Filter the related videos widget
  5.      *
  6.      * @param array $args
  7.      * @param string $widget_id
  8.      *
  9.      */
  10.     function videtube_filter_related_widget_args( $args, $widget_id = '' ) {
  11.         global $post;
  12.        
  13.         if( ! is_singular( 'video' ) ){
  14.             return $args;
  15.         }
  16.        
  17.         if( false !== $related_posts = get_transient( 'related_posts_' . $post->ID ) ){
  18.             $args['post__in'] = $related_posts;
  19.         }
  20.         else{
  21.             $maybe_posts = new WP_Query( $args );
  22.             if( $maybe_posts->have_posts() ){
  23.                 $args['post__in'] = wp_list_pluck( $maybe_posts->posts, 'ID' );
  24.             }
  25.             wp_reset_postdata();
  26.             wp_reset_query();
  27.             if( isset( $args['post__in'] ) && is_array( $args['post__in'] ) ){
  28.                 set_transient( 'related_posts_' . $post->ID, $args['post__in'] );
  29.             }
  30.         }
  31.         return $args;
  32.     }
  33.     add_filter( 'mars_related_widget_args' , 'videtube_filter_related_widget_args', 100, 2 );
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement