Skip Navigation

[Resolved] CPT Relationships Taxonomy acessess

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#2475967

I have created the many to many relationships. I want to display all the taxonomy (tags or category) of related post from other CPT.

For Eg. I have one CPT which is Organization, and second CPT is Announcements which has its own categories, the announcements are announced by org. now I want to display all the categories or tags in the front end under which each organization has made the announcements.

#2476023

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To display the data like taxonomies from the related posts, you'll need to use the view with the relationship query filter.
( ref: https://toolset.com/lesson-placement/lesson-placements-1729031-1730449/ )

Can you please share the link to the example page, where you'd like to show the related taxonomies? Also, do you have any example of the format in which you'd like to show these announcements?

I'll be in a better position to suggest the next steps, accordingly.

regards,
Waqar

#2476051

my website is in local development
but for eg on this link hidden link
grant type: is coming from below mentioned related grant opportunities announced by this agency on the same page i want them to dynamically be shown.

#2477505

Waiting for the response

#2477549

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

To show the list of related taxonomies, you'll need to register a custom shortcode. Assuming that the relationship slug on your website is 'organization-announcement' and the taxonomy slug is 'announcement-category', the code will look like this:


add_shortcode('get_related_taxonomy_items', 'get_related_taxonomy_items_func');
function get_related_taxonomy_items_func() {

	$current_post_id = do_shortcode('[wpv-post-id]');
	$relationship_slug = 'organization-announcement';
	$taxonomy_slug = 'announcement-category';
	$tax_links = array();

	$related_posts = toolset_get_related_posts($current_post_id,$relationship_slug,'parent',999999,0,array(),'post_id','child');

	foreach ($related_posts as $related_post ) {
		$terms = get_the_terms( $related_post, $taxonomy_slug );

		if ( $terms && ! is_wp_error( $terms ) ) {
			foreach ( $terms as $term ) {
				$term_link = get_term_link( $term );
				$tax_links[] = '<a href="'.$term_link.'">'.$term->name.'</a>';
			}
		}
	}
	
	$tax_links_output = implode(', ', array_unique($tax_links));
	return $tax_links_output;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

The shortcode will get the related announcement posts from the current organization post using the "toolset_get_related_posts" function ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts ) and then return their related taxonomies, making sure to remove any duplicates.

Next, to show the related taxonomy items on the single organization post, you can call this shortcode like this:


[get_related_taxonomy_items]

Important Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2478047

I have tried this method but got no results it's not returning anything. Just want to crosscheck by "relationship slug" you mean the relationship which I have created for organization and announcement and 'announcement-category' is taxonomy for announcements I have done this way but on front end Its not showing any category, it displays blank.

#2478187

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for this update and your understanding about the slugs is correct.

To troubleshoot why this custom shortcode is not working, I'll need to see how the related elements are set up in the admin area.

Can you please put your website on an online server and share its admin access details?

Alternatively, you can share the clone/snapshot of your localhost website, so that I can deploy it on my test server.
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the website's access details.

#2478997

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing the website's duplicator package.

During troubleshooting, I noticed that the order of posts in the relationship "Research Fundings & Organizations" was reversed from my test website, which changes the 'parent' and 'child' roles of the post types.
( the post type on the left is the parent and the one on the right is the child )

I changed the line, reversing the 'parent' and 'child' roles:


$related_posts = toolset_get_related_posts($current_post_id,$relationship_slug,'parent',999999,0,array(),'post_id','child');

To:


$related_posts = toolset_get_related_posts($current_post_id,$relationship_slug,'child',999999,0,array(),'post_id','parent');

And made sure that the custom code snippet was set as active ( screenshot: hidden link ) and it started working on the single "Funding Organizations" posts.
( screenshot: hidden link )

#2479845

My issue is resolved now. Thank you for the awesome support