Skip Navigation

[Resolved] Question about printing a taxonomy

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Jackie 6 years ago.

Assisted by: Nigel.

Author
Posts
#1194566

Hi there,

I'm building a course catalog for a college of art and design. We've got two custom post types: catalog and section (which is an instance of a third post type, course). Each section is associated with one or more categories. The categories are a custom taxonomy.

So, let's pretend I have a catalog with three sections:

+ Drawing Fundamentals. It's associated with the "Drawing" category.
+ Applied Techniques in Furniture Design. It's associated with the "Furniture Design and Wood" category.
+ Lapidary Workshop. It's associated with the "Jewelry" and "Workshops" categories.

In total, there are 31 categories, but the courses in this tiny hypothetical catalog are only associated with 4 of them. How can I create a listing (probably in a View) of just the 4 in-use categories?

Thank you!

Saul

#1195317

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Saul

This isn't possible without writing some custom code and using the relationships API, which I get the impression you are capable of.

You want to output (custom) categories, so you would create a View with this taxonomy as the content selection, and without any filters it will simply output all of the categories.

We need to tell it that it should only get the categories of a limited set of posts, specifically the section posts that are related to the current catalog post we are displaying.

So

1. use the wpv_filter_taxonomy_query API hook to intervene in the query that retrieves the categories (see https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query)

2. use the toolset_get_related_posts API hook to get the sections posts which are related to the current catalog post (see https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts)

3. pass this list of posts as an array of IDs to the object_ids taxonomy View query arguments, to limit the queried list of categories to those which are assigned to these posts (see https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/#parameters)

I'll let you put the pieces together, but here is a simple example of using the wpv_filter_taxonomy_query and *manually* specifying which posts to limit the search to:

toolset_snippet_security_check() or die('Direct access is not allowed');

function tssupp_custom_tax_query($tax_query_settings, $view_settings, $view_id) {

	if (in_array($view_id, array(308))) { // Edit ID of View to apply to

		$tax_query_settings['object_ids'] = array(99, 199, 299); // array of post IDs

	}

	return $tax_query_settings;
}
add_filter('wpv_filter_taxonomy_query', 'tssupp_custom_tax_query', 101, 3);

And as the documentation is a little opaque, here is an example of using toolset_get_related_posts. Note that although with many-to-many relationships the post types are effectively on a par, the API still adopts the custom of parent and child to describe the left and right sides of the relationship.

$children = toolset_get_related_posts(
	$parent_id, // ID of starting post
	$relationship_slug,
	array(
		'query_by_role' => 'parent', // start from the parent
		'limit'			=> 999,
		'return'		=> 'post_id', // return an array of post ID's
		'role_to_return'	=> 'child' // return the child
	)
);

Let me know how you get on.

#1195722

Hey Nigel,

Thanks for your response. You're correct in surmising that I'm capable, and unafraid, of writing some custom code and using the relationships API.

After reviewing your proposed approach, this seems like a whole lot of work. Why not just execute a custom SQL query inside a shortcode, and let the shortcode do all of the heavy lifting? I'm imagining a query (as pseudo-code) like so...

select * from categories where category_id in (select distinct category_id from sections where catalog_id = 9999) ;

I'm aware the above is very flawed, even by the standards of pseudo-code. The posts are going to be stored in wp_posts, taxonomy in wp_terms, etc. And Toolset stores a bunch of data in tables named wp_toolset_*.

I just think it'll be much quicker to write that SQL statement and parse its results than bother with the API calls. What do you think?

Saul

#1196047

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You could write a custom SQL query directly for this, but you'll need to study the table structure for taxonomies and for toolset relationships (wp_toolset_associations is the table that stores the actual connections), but once you step out of using normal WordPress queries combined with the Toolset API there's not much more help I can give you, I'm afraid.

#1198215

Thanks for the pointers regarding the toolset relationships. And here our paths diverge!

Saul