Skip Navigation

[Resolved] Retrieving the taxonomy of a post within a loop via wpv-view shortcode

This support ticket is created 3 years, 4 months 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 3 voices.

Last updated by Saul Baizman 3 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#2298811

Hi there,

I've written some custom PHP to loop through a set of posts and retrieve custom taxonomy terms related to each post by running a view via the

wpv-view

shortcode. The issue is that I am unsure how to specify the post ID so that the taxonomy terms are specific to a given post.

Pseudo-code looks like this:

foreach ( $post as $post_id => details)
{
$taxonomy_terms = do_shortcode('[wpv-view name="view-of-learner-types"]') ;
// output terms
}

Unfortunately, this just retrieves a list of all terms in the taxonomy.

I think I need to adjust the Query Filter in my view "view-of-learner-types" to limit it to the given

$post_id

, but I'm not sure how. Does this question make sense?

Saul

#2299067

Nigel
Supporter

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

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

Hi Saul

I'm not sure I follow exactly what you are aiming to do, but on the specific question of how to pass a post ID value to a View to use in a Query Filter, you can add shortcode attributes to the View shortcode and then specify the attribute in the Query Filter for the value source.

See https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/

#2299077
taxonomy term filter.png

Nigel,

Thanks for your response. I'm aware that when a View queries posts, it's possible to pass one or more Post IDs via a shortcode attribute ([wpv-view name="whatever" ids="56,23"]). But I'm querying a *taxonomy*, and the Query Filter doesn't include the same options to pass a Post ID via a shortcode attribute. To be clear, I can pass Term IDs, but not Post IDs. See the attached screenshot.

To clarify what I'm trying to do, I am iterating through a set of posts and trying to output the taxonomy terms of a custom taxonomy of each post. I am very aware that I can use the WordPress function get_the_terms() to do this, but I was trying to minimize the amount of custom PHP and author the loop in the Toolset interface, so I created a View to query the taxonomy terms. The issue is that unlike with get_the_terms(), I can't limit the View to a specific post. Does that make sense?

Saul

#2299257

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Saul,

Is it a case where you want to display taxonomy of the current post being displayed?

To clarify what I'm trying to do, I am iterating through a set of posts and trying to output the taxonomy terms of a custom taxonomy of each post

Are you iterating through the posts using a view?

Please let me know.
Thanks,
Shane

#2299261

Shane,

Thanks for your reply.

> Is it a case where you want to display taxonomy of the current post being displayed?

No, this is not the case. I've created a regular WP page and am using a shortcode to run some custom PHP code that retrieves some custom posts and then displays them. The "current post" is that regular page and does not contain the taxonomy information; that resides with the custom posts.

> Are you iterating through the posts using a view?

No, I am not. I am iterating through them via a foreach loop in PHP.

Saul

#2299923

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Saul,

Thank you for the clarity.

I understand what you're trying to achieve now. In such a case our view doesn't provide an open filter for you to manually specify the post ID to display the taxonomies.

You will need to call for the taxonomies manually using the get_the_terms() function.
https://developer.wordpress.org/reference/functions/get_the_terms/

Thanks,
Shane

#2299933

Thanks for your response, Shane. I'd love to see this as a feature request, if possible. It will allow people to retrieve taxonomy terms for a given post ID or IDs.