Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 23rd, 2012  |  syntax: PHP  |  size: 2.59 KB  |  views: 67  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Posts orderby filter.  The filter will be added using pre_get_posts outside the class
  3.  *  using the pre_get_posts action allows us to do checks for what page etc...
  4.  * @return string, new MySQL ORDER BY clause
  5.  */
  6. function mouldings_order_by() {
  7.         global $wpdb;
  8.         return $wpdb->prepare( "$wpdb->postmeta.meta_value+0 DESC, post_title DESC" );
  9. }
  10.  
  11. function mouldings_sort($query) {
  12.         if ($query->is_main_query() && is_tax(array('wood_types','profile_categories','combination_categories'))) {
  13.                 global $mouldings_options;
  14.  
  15.                 $number_columns = $mouldings_options['profile_item_columns'];
  16.                 $posts_per_page = mouldings_word_to_int($number_columns) * apply_filters('mouldings_row_size', 3);
  17.  
  18.                 // run through each taxonomy possibility and set the $taxonomy and $terms variable to the appropriate values
  19.                 if (is_tax('wood_types')) {
  20.                         $taxonomy = 'wood_types';
  21.                         $terms = $query->query_vars['wood_types'];
  22.                 }
  23.                 elseif (is_tax('profile_categories')) {
  24.                         $taxonomy = 'profile_categories';
  25.                         $terms = $query->query_vars['profile_categories'];
  26.                 }
  27.                 elseif (is_tax('combination_categories')) {
  28.                         $taxonomy = 'combination_categories';
  29.                         $terms = $query->query_vars['combination_categories'];
  30.                 }
  31.  
  32.                 $tax_query = array(
  33.                         array(
  34.                                 'taxonomy' => $taxonomy,
  35.                                 'field' => 'slug',
  36.                                 'terms' => $terms,
  37.                                 'include_children' => false
  38.                         )
  39.                 );
  40.  
  41.                 //print_r($query->query_vars);
  42.  
  43.                 if (is_tax('wood_types')) {
  44.                         $query->set('post_type',array('moulding_profiles','moulding_combination'));
  45.                 }
  46.  
  47.                 $query->set('posts_per_page', $posts_per_page);
  48.                 $query->set('tax_query', $tax_query);
  49.                 $query->set('meta_key', '_mouldings_dimensions_height');
  50.                 $query->set('order', 'DESC');
  51.                 //$query->set('orderby','meta_value_num');
  52.                 //
  53.                 /** remove_filter() is used to prevent this affecting additional queries on the page */
  54.         remove_filter( current_filter(), __FUNCTION__ );
  55.  
  56.         add_filter('posts_orderby','mouldings_order_by');
  57.  
  58.         }
  59.         if ((isset($_GET['sort']) && isset($_GET['order']) && $query->is_main_query() && is_tax(array('wood_types','profile_categories','combination_categories')))
  60.                 || (isset($_GET['sort']) && isset($_GET['order']) && is_tax(array('wood_types','profile_categories','combination_categories')) && isset($query->query_vars['post_type']) && ($query->query_vars['post_type'] == 'moulding_profiles' || $query->query_vars['post_type'] == 'moulding_combination'))) {
  61.                 $query->set('meta_key', urldecode($_GET['sort']));
  62.                 $query->set('sort', urldecode($_GET['sort']));
  63.                 $query->set('order', urldecode($_GET['order']));
  64.         }
  65.  
  66. }
  67. add_action('pre_get_posts','mouldings_sort');