Advertisement
developerjustin

Untitled

May 1st, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1.  
  2. function RelatedPosts($post)
  3. {
  4.     $id = $post->id;
  5.    
  6.     $title = $post->post_title;
  7.     $type = $post->post_type;
  8.    
  9.     $tags = get_the_tags($id);
  10.            
  11.    
  12.     $queryTags = array();
  13.     $postIDArray = array();
  14.  
  15.  
  16.     if(is_array($tags)){
  17.         foreach($tags as $tag)
  18.         {
  19.             //Comment out to exclude tags
  20.             array_push($queryTags,strtolower($tag->slug));
  21.         }      
  22.     }
  23.     else{
  24.         $titleArray = explode(" ", $title);
  25.         foreach($titleArray as $tag)
  26.         {
  27.             //Comment out to exclude title 
  28.             array_push($queryTags,strtolower($tag));
  29.         }          
  30.     }
  31.  
  32.     $filterArray = array(
  33.         "of",
  34.         "and",
  35.         "is",
  36.         "its",
  37.         "a",
  38.         "the",
  39.         "am",
  40.         "are",
  41.         "as",
  42.         "we",
  43.         "be",
  44.         "for",
  45.         "you",
  46.         "in",
  47.         "than",
  48.         "to",
  49.         "me",
  50.         "this",
  51.         "that",
  52.         "it"
  53.         );
  54.  
  55.  
  56.     foreach($queryTags as $tag)
  57.     {
  58.         foreach($filterArray as $filter)
  59.         {
  60.             if($tag==$filter)
  61.             {                              
  62.                 $index = array_search($filter,$queryTags);
  63.                 if($index !== FALSE){
  64.                     unset($queryTags[$index]);
  65.                 }                              
  66.             }
  67.         }      
  68.     }
  69.    
  70.     $queryTagCSV = implode(",", $queryTags);   
  71.  
  72.     if(!$tags)
  73.     {  
  74.         $query = new WP_Query(array(
  75.             'post_type' => $type,
  76.             'posts_per_page' => '3',
  77.             'no_found_rows' => 1,  
  78.             'orderby' => 'rand'                    
  79.         ));
  80.     }
  81.    
  82.  
  83.     else
  84.     {  
  85.         $query = new WP_Query(array(
  86.             'post_type' => $type,
  87.             'posts_per_page' => '3',
  88.             'no_found_rows' => 1,  
  89.             'orderby' => 'rand',                   
  90.             'tag' => $queryTagCSV                      
  91.         ));
  92.     }  
  93.  
  94.     return $query;     
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement