0

Using get_terms I've created three lists of term links. What currently happens if I click on a term is to see the archive for that term, what I'm attempting to achieve now is when I click a second term I want to see an archive for both terms not just the latest term. Basically creating some level of filtering posts.

My first guess is that I have to set some variable to affect the query, any suggestions on how to go about this? Or is there a better way of achieving this?

<h2>Filter By Deployment:</h2>
<?php
$deployments = get_terms( 'deployments', array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1) );
    echo '<ul>';
    foreach ( $deployments as $deployment ) {
        echo '<li><a href="'.get_term_link($deployment->slug, 'deployments').'">'.$deployment->name.'</a></li>';
    }
    echo '</ul>';
?>


<h2>Filter By Category:</h2>
<?php
$categories = get_terms( 'category', array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1) );
    echo '<ul>';
    foreach ( $categories as $category ) {
        echo '<li><a href="'.get_term_link($category->slug, 'category').'">'.$category->name.'</a></li>';
    }
    echo '</ul>';
?>


<h2>Filter By Keyword:</h2>
<?php
$tags = get_terms( 'post_tag', array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1) );
    echo '<ul>';
    foreach ( $tags as $tag ) {
        echo '<li><a href="'.get_term_link($tag->slug, 'post_tag').'">'.$tag->name.'</a></li>';
    }
    echo '</ul>';
?>
4
  • If you click now you will go to the tag archive. You can't be on 2 tag archives at once. So the filtering will have have to go to a custom page, possibly with $_GET params
    – janw
    Dec 18, 2012 at 15:07
  • My thought to deal with that was to delete the tag.php & category.php archives so that all would default to archive.php
    – Chuck
    Dec 18, 2012 at 15:15
  • That does trigger that behavious but that doesn't fix how you send over your params
    – janw
    Dec 18, 2012 at 15:36
  • I did exactly this is a recent project. What I did is use a custom page and set the filter params in the url then use $_GET to rerun the query based on those filters. I'll post an answer later if no one else has posted a similar answer.
    – Cai
    Feb 12, 2016 at 8:09

2 Answers 2

-1

Have you tried out isotope? I am working on a project that needs a similar function. So far it's the closest I've found that will sort/filter posts.

The link - http://isotope.metafizzy.co/

Here's some questions I had about implementing it with wordpress, and was able to do so to certain extent. It can help get you started -

https://stackoverflow.com/questions/13956593/unable-to-make-isotope-filter-sort-posts-in-wp

Isotope Drop Down Categories Filter Function

1
  • That looks like something I'll have to try, I've been moved off that project until the new year, but I'll be sure to come back and let you know how it worked. Thanks
    – Chuck
    Dec 23, 2012 at 14:33
-1

Just try your condition like below.
I think $query->is_post_type_archive() will not be working properly. I have not tried that so not sure. Directly call is_post_type_archive() and is_page() function.

if ( ( is_post_type_archive( 'question' ) || is_page(3001) ) && $query->is_main_query() ) {
        $query->set( 'post_type', 'question' );
    }

1
  • Don't just dump code as an answer. Please explain what your code does and why it should work. Please edit your answer May 9, 2014 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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