logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Narrow down search results

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 = "Movies"
Taxonomy = "Genre"
Taxonomy = "Director"

So if someone is on page that shows all of the posts (10 per page) in the Genre "Comedy", on the sidebar will list all of the taxonomy terms for Directors. Clicking on a link for one of the terms, example "Judd Apatow", 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.

Anthony Moore | 01/22/13 at 1:58am Edit
Tutorial: How to assign prize money

Previous versions of this question: 01/22/13 at 2:00am | 01/22/13 at 3:08am | 01/22/13 at 3:09am | 01/31/13 at 5:57am

(16) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/22/13
    2:08am
    Maor Barazany says:

    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 :
    http://wordpress.org/extend/plugins/faceted-search/
    http://wordpress.org/extend/plugins/faceted-search-widget/

  • avatar
    Last edited:
    01/22/13
    7:24am
    Arnav Joy says:

    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

    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.

  • avatar
    Last edited:
    01/22/13
    8:06am
    Francisco Javier Carazo Gil says:

    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.

  • avatar
    Last edited:
    01/22/13
    10:21am
    John Cotton says:

    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 );


  • avatar
    Last edited:
    01/22/13
    2:49am
    Anthony Moore says:

    Ideally I would not like to use a plugin.

  • avatar
    Last edited:
    01/22/13
    10:23am
    John Cotton says:

    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.

  • avatar
    Last edited:
    01/22/13
    2:22pm
    Anthony Moore says:

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

  • avatar
    Last edited:
    01/22/13
    2:50pm
    Anthony Moore says:

    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?

  • avatar
    Last edited:
    01/22/13
    3:36pm
    Francisco Javier Carazo Gil says:

    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' );

  • avatar
    Last edited:
    01/22/13
    4:11pm
    Anthony Moore says:

    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/

  • avatar
    Last edited:
    01/22/13
    4:15pm
    John Cotton says:

    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.

  • avatar
    Last edited:
    01/22/13
    6:38pm
    Anthony Moore says:

    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?

  • avatar
    Last edited:
    01/22/13
    7:07pm
    John Cotton says:

    Nope.

  • avatar
    Last edited:
    01/23/13
    4:42pm
    Anthony Moore says:

    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' ?

  • avatar
    Last edited:
    01/23/13
    4:58pm
    John Cotton says:

    What is ''ex_genre' ?


    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 );

  • avatar
    Last edited:
    01/23/13
    4:59pm
    John Cotton says:

    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.

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.