Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Narrow down search results WordPress

I am trying to have some template pages that have a bit of a "Narrow This Search" function.

Lets say I have my post type and taxonomies set up like this:

Post Type = "<strong>Movies</strong>"
Taxonomy = "<strong>Genre</strong>"
Taxonomy = "<strong>Director</strong>"

So if someone is on page that shows all of the posts (10 per page) in the <strong>Genre </strong>"<em>Comedy</em>", on the sidebar will list all of the taxonomy terms for <strong>Directors</strong>. Clicking on a link for one of the terms, example "<em>Judd Apatow</em>", will go to a page that now displays post in the Comedy genre and directed by Judd Apatow.

Anyone know how to do this?

This is what I currently Have:

On the sidebar in my taxonomy_genre.php template I have:





...Main Article Stuff
<aside>
<div class="widget">
<a class="widget-title title" href="#">Directors</a>

<ul>
<?php
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms('director', $args);
foreach($terms as $term) {
echo '<li>';
echo '<a href="' . get_bloginfo("url").'/movies/directors/'. $term->slug. '" title="' . sprintf( __( "View: %s" ), $term->name ) . '" ' . '>' . $term->name.'</a>';
echo '</li>';
}
?>
</ul>
</div>

</aside>


So clicking on a director will take me to the taxonomy_directors.php template, and will display all of the movies in the Comedy genre and also the director clicked on from the previous page.

Answers (4)

2013-01-22

Maor Barazany answers:

You may write your own code to do that, but it might be easier for you to try a plugin of "faceted search"
Take a look at these :
[[LINK href="http://wordpress.org/extend/plugins/faceted-search/"]]http://wordpress.org/extend/plugins/faceted-search/[[/LINK]]
[[LINK href="http://wordpress.org/extend/plugins/faceted-search-widget/"]]http://wordpress.org/extend/plugins/faceted-search-widget/[[/LINK]]


Anthony Moore comments:

Ideally I would not like to use a plugin.

2013-01-22

Arnav Joy answers:

so if so if you click on "Judd Apatow" at sidebar then you will get list of posts in "Judd Apatow" term , right?

i am asking this question because i am not clear with your following statement

<blockquote>So clicking on a director will take me to the taxonomy_directors.php template, and will display all of the movies in the Comedy genre and also the director clicked on from the previous page. </blockquote>


Anthony Moore comments:

Yes it would show all of the posts with the taxonomy term "Judd Apatow" and the taxonomy term "Comedy".

2013-01-22

Francisco Javier Carazo Gil answers:

Hi Anthony,

Maybe the best option is create a page with a template (this template) and pass a parameter to it using a GET: http://codex.wordpress.org/Function_Reference/add_query_arg.

You take the parameter and then in the template make the search.


Anthony Moore comments:

Hi Francisco,

I have read up on the add_query_arg function. Where do I add this in? My taxonomy_directors.php file?

Does this need to go before the loop, or does that not matter?


Francisco Javier Carazo Gil comments:

Anthony,

You have to add the parameter in the call to the page (i. e. an GET ?parameter=value) without using this function if you want, and then you can collect it into the template: get_query_var( 'parameter' );

2013-01-22

John Cotton answers:

You should set up some custom rewrites and custom query vars for the urls eg

$new_rules['^movies/directors/([^/]+)/([^/]+)$'] = 'index.php?Director=$matches[1]&ex_genre=$matches[2]';

You have to have the ex_genre query var otherwise WP will get confused about which taxonomy you are looking at. Of course you could also set up

$new_rules['^movies/genre/([^/]+)/([^/]+)$'] = 'index.php?Genre=$matches[1]&ex_directors=$matches[2]';

And have it work the other way around but I think you'd get an SEO penalty for the duplicate pages (in that /movies/directors/bob/comedy would produce the same page as /movies/comedy/directors/bob).

Then in you taxonomy_directors.php file, you'd do:


global $wp_query;
if( $genre = get_query_var('ex_genre') {
// Amend query_posts to have the extra taxonomy:
$wp_query->query_vars->tax_query['relation'] = 'AND';
$wp_query->query_vars->tax_query[] = array( 'taxonomy' => 'genre', 'field' => 'slug', 'terms' => array( $genre );
}
query_posts( $wp_query->query_vars );



John Cotton comments:

PS you might have to play around a bit more with $wp_query->query_vars in case just adding the extra genre messes up the query, but if you've got a straight taxonomy_template it should work as I showed.


Anthony Moore comments:

I am guessing i put the custom rewrites in my .htaccess file. Or does this go into a function?

By default should this link work by displaying posts that have both taxonomies?


http://site.com/movies/?genre=comedy&director=bob

If it does not work when I type in that URL, is there an issue with my taxonomies set up?

I was looking at the examples in this article:

http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/


John Cotton comments:

Well you need to set proper permalinks first (%category%/%postname% is my personal preference).

The rewrites could go in the .htaccess, but are much better to go in your functions.php using the generate_rewrite_rules hook (see dozens of previous questions on here for more on that or the Codex). Ditto query_vars.

By default, any taxonomy_name.php file is going to show an archive list for that taxonomy. So that's why you need to use query_posts to modify the default behaviour.


Anthony Moore comments:

I need to have my permalinks set up like so (/%year%/%monthnum%/%postname%/), simply because that was the way it was set up on the old website for several years.

Does that cause a problem?


John Cotton comments:

Nope.


Anthony Moore comments:

For now I will not change my permalinks as I will play around with it once I get the function working. I actually have several taxonomies that will be incorporated.

In your example code to put in taxonomy_directors.php

global $wp_query;

if( $genre = get_query_var('ex_genre') {

// Amend query_posts to have the extra taxonomy:

$wp_query->query_vars->tax_query['relation'] = 'AND';

$wp_query->query_vars->tax_query[] = array( 'taxonomy' => 'genre', 'field' => 'slug', 'terms' => array( $genre );

}

query_posts( $wp_query->query_vars );



What is ''ex_genre' ?


John Cotton comments:

<blockquote>What is ''ex_genre' ?</blockquote>

It's a custom query var to avoid WP getting confused with having two taxpnomu qvs on the same page:


function add_query_vars( $query_vars ) {
$query_vars[] = 'ex_genre';

return $query_vars;
}
add_action( 'query_vars', 'add_query_vars', 10, 1 );


John Cotton comments:

Query vars are just name/value pairs on the query string that WP uses internal when it rewrites your custom url to index.php so that it knows what page/state has been requested.

By telling WP about your own custom ones, you keep things neat and WP plays ball with all sorts of URLs.