Almost 1000 views and not a single comment. Well, I also needed this and decided to do it. I shared the JavaScript and Wordpress code below for people in the distant future. This seems like a lot, but this is because I have defined some jQuery functions that you can use later with .extend . All it does is search for the select element (drop-down list) with the CSS class .content-filter .
Once discovered, it uses the drop-down identifier to set the GET variable to the value currently selected, then redirects to the same URL and adds these GET variables. For example, if the drop-down list identifier was product_filter , and this value was set to date , then it would set the GET variable product_filter=date . This is great because it does not care about your Wordpess details - all it cares about is the select element.
// A bunch of helper methods for reading GET variables etc from the URL jQuery.extend({ urlGetVars : function() { var GET = {}; var tempGET = location.search; tempGET = tempGET.replace('?', '').split('&'); for(var i in tempGET) { var someVar = tempGET[i].split('='); if (someVar.length == 2) { GET[someVar[0]] = someVar[1]; } } return GET; }, urlGetVar : function(name) { return $.urlGetVars()[name]; }, serializeUrlVars : function(obj) { var str = []; for(var p in obj) str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); return str.join("&"); }, currentUrl : function() { return window.location.href.slice(0,window.location.href.indexOf('?')); } }); // Adds functionality to filter content using a dropdown var ContentFilter = function ($) { $(document).ready(function() { // Return to a scroll position if exists var scroll = $.urlGetVar('scroll'); if (typeof scroll != 'undefined') { $(window).scrollTop(scroll); } // Prepare the filter dropdowns $('.content-filter').each(function(){ var me = $(this); // eg content-filter-product var id = me.attr('id'); // Refresh with selected filter on change var refresh = function() { var GET = $.urlGetVars(); GET[id] = me.val(); // Save scroll position, return to this position on load GET['scroll'] = $(window).scrollTop(); var newVar = $.currentUrl() + '?' + $.serializeUrlVars(GET); window.location = newVar; }; me.change(refresh); }); }); }(jQuery);
Now the wordpress code. We really need to generate select with some sort of identifier and set the class to .content-filter . This code asks for a post or product type message type and makes a select element. It then returns the GET variable for convenience, and if none of them are set, the default value is "latest." Note that the $fields array sets all the orderby values you want to support. You can always access it anywhere in the template using $_GET['product_filter'] or $_GET['post_filter'] depending on your type. This means that only one can exist on any given page, but you want jQuery to not know what to use otherwise. You can extend this code to set a user id or whatever you like later.
function ak_content_filter($post_type_id = 'post', &$filter_get_value, $echo = TRUE) { $dropdown = '<div class="content-filter-wrapper">'; // The dropdown filter id for this post type $filter_id = $post_type_id.'_filter'; // The actual dropdown $dropdown .= '<label for="'. $filter_id .'">Filter</label><select id="'. $filter_id .'" class="content-filter" name="'. $filter_id .'">'; // The available ways of filtering, to sort you'd need to set that in the WP_Query later $fields = array('date' => 'Newest', 'comment_count' => 'Most Popular', 'rand' => 'Random'); $filter_get_value = isset($_GET[$filter_id]) ? $_GET[$filter_id] : 'newest'; // default is 'newest' foreach ($fields as $field_value=>$field_name) { $dropdown .= '<option value="'. $field_value .'" '. selected($field_value, $filter_get_value, FALSE) .'>'. $field_name .'</option>'; } $dropdown .= '</select></div>'; // Print or return if ($echo) { echo $dropdown; } else { return $dropdown; } }
Now the interesting part is combining it on the content page. All our work pays off with some sweet and short code:
// This will fill $product_filter with $_GET['product_filter'] or 'newest' if it doesn't exist ak_content_filter('product', $product_filter); $args = array('post_type' => 'product', 'orderby' => $product_filter); // This is just an example, you can use get_pages or whatever supports orderby $loop = new WP_Query( $args ); // OR, to avoid printing: $dropdown = ak_content_filter('product', $product_filter, FALSE); // ... some code ... echo $dropdown;
I used a custom message type of type, but if you use a "post" just replace it. Maybe someone should do this in the plugin if they haven't been: P