0

I'm using category intersection pages for users to be able to filter posts by categories.

E.g. http://freemapsalgarve.com/category/destinations/alvor+bars-pubs/

Here the user is filtering on the "alvor" AND "bars-pubs" child categories of "destinations".

I want to be able to get the id's of the categories that are being filtered on the page so I can display them to the user but I am only able to get the first category.

$this_cat = get_category(get_query_var('cat'));

...returns the first category "alvor" in this example.

Does anyone know how I could find the both categories being filtered on here?

6
  • What are "category intersection pages"? This is no default WordPress vocabulary. Please elaborate.
    – kaiser
    Mar 10, 2013 at 20:00
  • In WordPress you can display the intersection or union of two taxonomies using the + or , operators in the url: E.g. /categories/a+b - would show all posts in category A AND B. Some more info here - ryan.boren.me/2007/10/01/taxonomy-intersections-and-unions
    – David
    Mar 10, 2013 at 20:47
  • Oh, you're simply talking about a tax_query. Why didn't you say that. Ryan Boran uses a wired vocabulary for a core dev. Oh and this post is from WP version 2.3. Things are quite different these days. set_query_var() for example was replaced using $query->set(), etc. Just var_dump( $GLOBALS['wp_query'] ); and post a link to a pastebin (or similar) here.
    – kaiser
    Mar 10, 2013 at 21:00
  • Here's the output of var_dump: pastebin.com/DxyMPYxq - I can see that there are two category_name fields - one containing both category names and one containing only a single category name.
    – David
    Mar 12, 2013 at 9:46
  • David, could you please wrap this inside <pre> tags. This is not readable.
    – kaiser
    Mar 12, 2013 at 11:05

2 Answers 2

1

If you're in the loop, simply use get_categories().

1
  • This just lists out all the categories in the system - I want to know the names of the Categories being displayed on the Category intersection page being viewed - outside the loop.
    – David
    Mar 13, 2013 at 10:17
1

If you're not in the loop and want to get the queried terms, then there's no native API function as well as no WP_Query or WP_Tax_Query object method available (like for example get_queried_object()). You'll have to access it directly.

Example of what the tax_query looks like (provided by the OP):

["tax_query"]=>
  object(WP_Tax_Query)#282 (2) {
    ["queries"]=>
    array(2) {
      [0]=>
      array(5) {
        ["taxonomy"]=>
        string(8) "category"
        ["terms"]=>
        array(1) {
          [0]=>
          string(5) "alvor"
        }
        ["include_children"]=>
        bool(true)
        ["field"]=>
        string(4) "slug"
        ["operator"]=>
        string(2) "IN"
      }
      [1]=>
      array(5) {
        ["taxonomy"]=>
        string(8) "category"

Now we need to access that $wp_query object. We'll do that with a small plugin that will output the necessary parts.

<?php
defined( 'ABSPATH' ) or exit;
/* Plugin Name: (#90230) Get Taxonomy terms */
add_filter( 'parse_query', 'wpse90230_parse_query' );
function wpse90230_parse_query( $wp_query )
{
    // This is the tax_query/WP_Tax_Query object
    $tax_query = $wp_query->tax_query; 

    // Now we get the relation `AND`/`OR` so we have a possibilty to the tell the user
    // whether we are showing them a "match" or a "filtered" result
    $relation = 'AND' === $tax_query->relation ? "filtered" : "matched";

    // Then we're extracting the terms.
    // This gives us the terms as array sorted by taxonomy.
    foreach ( $tax_query->queries as $tax_query )
        $terms[ $tax_query["taxonomy"] ] = $tax_query["terms"];

    // Now we can loop through them:
    printf( 'You are viewing %s', get_post_type() );
    foreach ( $terms as $taxonomy => $terms )
        printf(
             '%s: %s'
            ,get_taxonomy( $taxonomy )->label
            ,join( ",", $terms )
        );
}
4
  • Thanks Kaiser - trying out your example at the top of my category page (below the header but before the loop) gives the following output: "You are viewing postCategories: bars-pubs" - it doesn't seem to be seeing the other category "Alvor" which should be there since the path is /category/destinations/alvor+bars-pubs/
    – David
    Mar 13, 2013 at 13:59
  • @David Please do a var_dump( $terms ) before the foreach() loop.
    – kaiser
    Mar 13, 2013 at 14:04
  • Here's the output: array(1) { ["category"]=> array(1) { [0]=> string(9) "bars-pubs" } } from this page - freemapsalgarve.com/category/destinations/alvor+bars-pubs
    – David
    Mar 13, 2013 at 16:30
  • In this case you seem to do several queries. And this query only holds the bars-pubs term... Seems like you're interfering earlier. I'll write an update. You can then use it and inspect the output.
    – kaiser
    Mar 13, 2013 at 18:22

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.