Advertisement
Guest User

WP add_filter/custom query

a guest
Jan 31st, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. //following is in functions.php (inside the theme folder)
  2.  
  3. if (isset($_GET['sort'])) {
  4. $sort = $_GET['sort'];
  5. }
  6.  
  7. add_filter('posts_join', 'votes_join');
  8. add_filter('posts_where', 'votes_where');
  9. add_filter('posts_orderby', 'votes_orderby');
  10.  
  11. function votes_join($join)
  12. {
  13.         global $sort;
  14.         if (is_front_page() && !empty($sort)) {
  15.            if ($sort == 1 || $sort == 2){
  16.               $join = "LEFT JOIN wp_xbhuui_wpv_voting ON wp_xbhuui_posts.ID = wp_xbhuui_wpv_voting.post_id";
  17.            }
  18.         }
  19.  
  20.         return $join;
  21. }
  22.  
  23. function votes_where($where)
  24. {
  25.         global $sort;
  26.         if (is_front_page() && !empty($sort)) {
  27.  
  28.         if ($sort == 1) {
  29.           $where .= "AND (post_date > DATE_SUB(NOW(), INTERVAL 1 DAY) OR wp_xbhuui_posts.ID = '31' OR wp_xbhuui_posts.ID = '2' OR wp_xbhuui_posts.ID = '5' OR wp_xbhuui_posts.ID = '1018')";
  30.         }
  31.         elseif ($sort == 2) {
  32.           $where .= "AND (post_date > DATE_SUB(NOW(), INTERVAL 7 DAY) OR wp_xbhuui_posts.ID = '31' OR wp_xbhuui_posts.ID = '2' OR wp_xbhuui_posts.ID = '5' OR wp_xbhuui_posts.ID = '1018')";
  33.         }
  34.         else {
  35.         //no sort selected, do nothing
  36.         }}  
  37.         return $where;  
  38. }
  39.  
  40. function votes_orderby ($orderby)
  41. {
  42.         global $sort;
  43.         if (is_front_page() && !empty($sort)) {
  44.  
  45.         if ($sort == 1 || $sort == 2){
  46.         $orderby = "vote_count DESC";
  47.         }
  48.         }
  49.         return $orderby;
  50. }
  51.  
  52. //following is in top of index.php template file
  53. global $sort;
  54. $sort = 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement