Guest User

Untitled

a guest
Aug 24th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. add_action('pre_get_posts', 'td_modify_main_query_for_category_page');
  2. function td_modify_main_query_for_category_page2($query) {
  3.  
  4.  
  5. //checking for category page and main query
  6. if(!is_admin() and is_category() and $query->is_main_query()and (is_front_page() and is_home())) {
  7.  
  8. // get the category object - with or without permalinks
  9. if (empty($query->query_vars['cat'])) {
  10. td_global::$current_category_obj = get_category_by_path(get_query_var('category_name'), false); // when we have permalinks, we have to get the category object like this.
  11. } else {
  12. td_global::$current_category_obj = get_category($query->query_vars['cat']);
  13. }
  14.  
  15.  
  16. // we are on a category page with an ID that doesn't exists - wp will show a 404 and we do nothing
  17. if (is_null(td_global::$current_category_obj)) {
  18. return;
  19. }
  20.  
  21.  
  22. //get the number of page where on
  23. $paged = get_query_var('paged');
  24.  
  25. //get the `filter_by` URL($_GET) variable
  26. $filter_by = get_query_var('filter_by');
  27.  
  28. //get the limit of posts on the category page
  29. $limit = get_option('posts_per_page');
  30.  
  31.  
  32. //echo $filter_by;
  33. switch ($filter_by) {
  34. case 'featured':
  35. //get the category object
  36. $query->set('category_name', td_global::$current_category_obj->slug);
  37. $query->set('cat', get_cat_ID(TD_FEATURED_CAT)); //add the fetured cat
  38. break;
  39.  
  40. case 'popular':
  41. $query->set('meta_key', td_page_views::$post_view_counter_key);
  42. $query->set('orderby', 'meta_value_num');
  43. $query->set('order', 'DESC');
  44. break;
  45.  
  46. case 'popular7':
  47. $query->set('meta_key', td_page_views::$post_view_counter_7_day_total);
  48. $query->set('orderby', 'meta_value_num');
  49. $query->set('order', 'DESC');
  50. break;
  51.  
  52. case 'review_high':
  53. $query->set('meta_key', td_review::$td_review_key);
  54. $query->set('orderby', 'meta_value_num');
  55. $query->set('order', 'DESC');
  56. break;
  57.  
  58. case 'random_posts':
  59. $query->set('orderby', 'rand');
  60. break;
  61. }//end switch
  62.  
  63.  
  64. // how many posts are we showing in the big grid for this category
  65. $offset = 5;
  66.  
  67.  
  68. // offset + custom pagination - if we have offset, WordPress overwrites the pagination and works with offset + limit
  69. if(empty($query->is_feed)) {
  70. if ( ! empty( $offset ) and $paged > 1 ) {
  71. $query->set( 'offset', $offset + ( ( $paged - 1 ) * $limit ) );
  72. } else {
  73. $query->set( 'offset', $offset );
  74. }
  75. }
  76. //print_r($query);
  77. }//end if main query
  78. }
Advertisement
Add Comment
Please, Sign In to add comment