Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to give the option to filter the index page and every archive page for a category by things like newest, top-rated, most-viewed. I know how to filter a loop for each of those but I need a solution for switching to each filtered loop.

share|improve this question
    
Close voted as not constructive: »This question is not a good fit to our Q&A format. We expect answers to generally involve facts, references, or specific expertise; this question will likely solicit opinion, debate, arguments, polling, or extended discussion.« –  kaiser Apr 4 '12 at 13:51
    
I changed the last question so it should be good. Im just looking for a solution that is efficient. –  Pollux Khafra Apr 4 '12 at 13:56
    
How about linking the question and telling "it is a follow up"? –  kaiser Apr 4 '12 at 14:05
    
What you're asking involves multiple questions and research avenues, e.g. how to track view counts, how to rate posts, how to show the posts with the most views for solution X, how to show posts ordered by rank etc etc, I can easily see 3 questions here, not one –  Tom J Nowell Apr 4 '12 at 14:17
    
Oh sorry I know how to do all the filtering and should have stated that. I just need to know a way to switch back and forth to loops with different filters. –  Pollux Khafra Apr 4 '12 at 14:47

1 Answer 1

up vote 1 down vote accepted

I would recommend defining a query var to define your custom filters, and then pass the (sanitized) value of that query var to query_posts() (to alter the primary Loop, if that is your ojbective).

First (in functions.php), define your query var:

<?php
function wpse47974_queryvars( $qvars )
{
$qvars[] = 'wpse47974_filter';
return $qvars;
}
add_filter( 'query_vars', 'wpse47974_queryvars' );
?>

Next, in the template, query your query var. I'll assume that you have a pre-defined set of filters, defined in an array, $wpse47974_valid_filters:

<?php
// Query Tag Filter
$wpse47974_filter = '';
if ( isset( $wp_query->query_vars['wpse47974_filter'] ) && in_array( $wp_query->query_vars['wpse47974_filter'], $wpse47974_valid_filters ) ) {
    $wpse47974_filter = $wp_query->query_vars['wpse47974_filter'];
}
?>

Next (somewhere), you'll need to define your query parameters based on your filter types. I leave this as an exercise for the OP; however, in the following code, these query parameters are represented by "$wpse47974_filter_args['key'] => 'value';", and we add them to an array, here (in the template file):

<?php
// Define an empty array to hold our filter parameters.
// This needs to be an array, in order not to generate 
// errors later in the array_merge(), if no filter parameters
// are being applied to the default query
$wpse47974_filter_args = array();

// If our query var is set, apply the filter parameter
if ( '' != $wpse47974_filter  ) {
    $wpse47974_filter_args['key'] = 'value';
};    
?>

Next, still in the template file, merge the default query with our filter parameters above, to modify query_posts() (note: before outputting the Loop):

<?php
// Globalize $wp_query
global $wp_query;
// Merge the default query with our filter parameters
$wpse47974_query_posts_args = array_merge( $wp_query->query, $wpse47974_filter_args );
// Modify the default query
query_posts( $wpse47974_query_posts_args );
?>

Now, the primary loop query is modified as per your filter.

Edit

I forgot an important part: how to get the query vars into the URL!

In your template, where you have your filter links (or buttons, or whatever), I assume you have an HTML anchor. Set the href attribute as follows, using the add_query_arg() function:

<a href="<?php echo add_query_arg( array( 'wpse47974_filter' => 'value' ) ); ?>">FILTER NAME</a>

Now, when you click the link, the page is reloaded, with your query var appended to the URL.

share|improve this answer
    
Thanks for taking the time to explain this. Im alittle confused about applying it in a template. So say I have an index loop that shows all the latest posts and 2 buttons that I want a user to be able to click to change the loop to sort by either top-rated or most-viewed. How do I go about doing that? –  Pollux Khafra Apr 4 '12 at 17:19
    
Whoops! I forgot to add that part. See edited answer. –  Chip Bennett Apr 4 '12 at 17:34
    
This is perfect for me Chip Thanks a ton! –  Pollux Khafra Apr 4 '12 at 17:42
    
Glad it helped! –  Chip Bennett Apr 4 '12 at 17:43
    
Ughh I must be doing something wrong. It says unexpected double arrow on 17 of this pastebin. pastebin.com/4Hxn1eE8 –  Pollux Khafra Apr 4 '12 at 19:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.