• capcar

    (@capcar)


    Greetings all, I am using WordPress posts to create a huge collection of vendor pages, and these pages are tagged with 2 different taxonomies: location and service. The taxonomies are working wonderfully by themselves, however, I now need the ability to cross-reference them. In other words, if someone clicks the listing for Alabama, I want a list of service tags that can narrow down that listing further.

    My question is: If there a way to create an array of only the terms that are attached to the the posts in the current “query_posts()” set? In this example, I don’t want to list every service tag again (there are hundreds), I only want those that are pertinent to the current “Alabama” set.

    For example, if I have the following posts:

    Alabama Post 1: tagged with Kitchen, Garage
    Alabama Post 2: tagged with Kitchen, Bedroom
    Alabama Post 3: tagged with Office, Closet

    …I am looking to create a term array that only contains:

    Kitchen, Garage, Bedroom, Office, Closet

    I feel this solution is just barely eluding me and would appreciate your input. Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • vtxyzzy

    (@vtxyzzy)

    I have not tested this, but this is my first thought:

    Loop through the posts and collect an array of IDs, then do a query on the service tag using a posts__in parameter.

    Thread Starter capcar

    (@capcar)

    Thought about doing this, but the results are paginated, so I can’t loop through the full results on one page… though I guess I could run the loop twice – once just to collect the ID’s on a full result.

    In the end, I chose to make a custom SQL query – basically taking the page query (which I got using $GLOBALS[‘wp_query’]->request) and creating a SELECT statement for the terms whose relationship ID’s that are IN that query. Runs rather slow, however, so I may try your suggestion. Thanks!

    vtxyzzy

    (@vtxyzzy)

    Rather than retrieving the entire posts with your custom query, just select the ID. That should run faster.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get terms for array of posts’ is closed to new replies.