Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('pre_get_posts', 'td_modify_main_query_for_category_page');
- function td_modify_main_query_for_category_page2($query) {
- //checking for category page and main query
- if(!is_admin() and is_category() and $query->is_main_query()and (is_front_page() and is_home())) {
- // get the category object - with or without permalinks
- if (empty($query->query_vars['cat'])) {
- 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.
- } else {
- td_global::$current_category_obj = get_category($query->query_vars['cat']);
- }
- // we are on a category page with an ID that doesn't exists - wp will show a 404 and we do nothing
- if (is_null(td_global::$current_category_obj)) {
- return;
- }
- //get the number of page where on
- $paged = get_query_var('paged');
- //get the `filter_by` URL($_GET) variable
- $filter_by = get_query_var('filter_by');
- //get the limit of posts on the category page
- $limit = get_option('posts_per_page');
- //echo $filter_by;
- switch ($filter_by) {
- case 'featured':
- //get the category object
- $query->set('category_name', td_global::$current_category_obj->slug);
- $query->set('cat', get_cat_ID(TD_FEATURED_CAT)); //add the fetured cat
- break;
- case 'popular':
- $query->set('meta_key', td_page_views::$post_view_counter_key);
- $query->set('orderby', 'meta_value_num');
- $query->set('order', 'DESC');
- break;
- case 'popular7':
- $query->set('meta_key', td_page_views::$post_view_counter_7_day_total);
- $query->set('orderby', 'meta_value_num');
- $query->set('order', 'DESC');
- break;
- case 'review_high':
- $query->set('meta_key', td_review::$td_review_key);
- $query->set('orderby', 'meta_value_num');
- $query->set('order', 'DESC');
- break;
- case 'random_posts':
- $query->set('orderby', 'rand');
- break;
- }//end switch
- // how many posts are we showing in the big grid for this category
- $offset = 5;
- // offset + custom pagination - if we have offset, WordPress overwrites the pagination and works with offset + limit
- if(empty($query->is_feed)) {
- if ( ! empty( $offset ) and $paged > 1 ) {
- $query->set( 'offset', $offset + ( ( $paged - 1 ) * $limit ) );
- } else {
- $query->set( 'offset', $offset );
- }
- }
- //print_r($query);
- }//end if main query
- }
Advertisement
Add Comment
Please, Sign In to add comment