• Hello,

    I’m creating a directory of local vendors. The vendors are in different locations and offer different kinds of products and services.

    I have created a taxonomy called Locations and have some categories such as Automotive, Restaurants etc.

    I want the user to click on a Location and be directed to a list of categories, the blog posts within which only pertain to the location selected.

    Is this possible?

    Thank you for any assistance.

    • This topic was modified 2 years, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Moderator bcworkz

    (@bcworkz)

    wp_list_categories() will list all terms used in a particular taxonomy, but they are not restricted to terms assigned to posts in a different taxonomy such as locations. There is no direct relationship between different taxonomies. They are only related through posts with their respective terms.

    To get only category terms used in posts of a particular location, you’d first need to query for all posts in the location and compile a list of unique category terms assigned to the posts in the query results.

    The code to do so would look something like (in pseudo-code):

    $posts = get_posts(['location' => 'someplace',]);
    $collect = array();
    foreach ( $posts as $post ) {
      $cats = get_post_cats( $post, ['fields' => 'ids',]);
      foreach ( $cats as $cat ) {
         if ( ! in_array( $cat, $collect )) push( $collect, $cat );
      }
    }
    list_cats(['include' => $collect,]);
    Thread Starter bevyoung2013

    (@bevyoung2013)

    Hi, thank you for the pseudo-code.
    Unfortunately I wouldn’t know where to place the code or how to customize it.

    Can you provide any further direction or should I hire someone?

    Thanks,

    Moderator bcworkz

    (@bcworkz)

    Where depends on where you want the list to appear. One solution would be to create a custom page template.

    When you introduce files to a theme that’s subject to periodic updates, you should create a child theme to protect your added files.

    I believe my pseudo code is syntactically correct for PHP, I only made up a few fake function names. But equivalent real functions do exist. The passed parameters may need modification once the correct real functions are identified. I recommend finding the real functions in the WP code reference and substituting them in. Start typing the names I used into search and the auto-suggest feature will show you similar real functions. I think it’s worth a try before resorting to hiring someone.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Taxonomy and Category hierarchy’ is closed to new replies.