/**
* Posts orderby filter. The filter will be added using pre_get_posts outside the class
* using the pre_get_posts action allows us to do checks for what page etc...
* @return string, new MySQL ORDER BY clause
*/
function mouldings_order_by() {
global $wpdb;
return $wpdb->prepare( "$wpdb->postmeta.meta_value+0 DESC, post_title DESC" );
}
function mouldings_sort($query) {
if ($query->is_main_query() && is_tax(array('wood_types','profile_categories','combination_categories'))) {
global $mouldings_options;
$number_columns = $mouldings_options['profile_item_columns'];
$posts_per_page = mouldings_word_to_int($number_columns) * apply_filters('mouldings_row_size', 3);
// run through each taxonomy possibility and set the $taxonomy and $terms variable to the appropriate values
if (is_tax('wood_types')) {
$taxonomy = 'wood_types';
$terms = $query->query_vars['wood_types'];
}
elseif (is_tax('profile_categories')) {
$taxonomy = 'profile_categories';
$terms = $query->query_vars['profile_categories'];
}
elseif (is_tax('combination_categories')) {
$taxonomy = 'combination_categories';
$terms = $query->query_vars['combination_categories'];
}
$tax_query = array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms,
'include_children' => false
)
);
//print_r($query->query_vars);
if (is_tax('wood_types')) {
$query->set('post_type',array('moulding_profiles','moulding_combination'));
}
$query->set('posts_per_page', $posts_per_page);
$query->set('tax_query', $tax_query);
$query->set('meta_key', '_mouldings_dimensions_height');
$query->set('order', 'DESC');
//$query->set('orderby','meta_value_num');
//
/** remove_filter() is used to prevent this affecting additional queries on the page */
remove_filter( current_filter(), __FUNCTION__ );
add_filter('posts_orderby','mouldings_order_by');
}
if ((isset($_GET['sort']) && isset($_GET['order']) && $query->is_main_query() && is_tax(array('wood_types','profile_categories','combination_categories')))
|| (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'))) {
$query->set('meta_key', urldecode($_GET['sort']));
$query->set('sort', urldecode($_GET['sort']));
$query->set('order', urldecode($_GET['order']));
}
}
add_action('pre_get_posts','mouldings_sort');