Skip Navigation

[Resolved] Trying to Pull Taxonomy Terms Outside of a View

This thread is resolved. Here is a description of the problem and solution.

Problem:

The client wanted a user to search for a dog breed, and whatever results show up, the showcase view will show the showcase dog that is relative to that search.

Solution:

You can create another view with same filters, see details here:

https://toolset.com/forums/topic/trying-to-pull-taxonomy-terms-outside-of-a-view/#post-1140973

Relevant Documentation:

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
- 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/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 2 voices.

Last updated by zacharyL 6 years ago.

Assisted by: Luo Yang.

Author
Posts
#1136934
screenshot.jpg

I have an archive page with two views. One that is designed to present a showcase dog, and the other which acts as the archive view of the page.

I'm trying to load the content of the first view dynamically by loading the taxonomy term(s) that appears in the second view. The client wanted a user to search for a dog breed, and whatever results show up, the showcase view will show the showcase dog that is relative to that search. For instance, if someone searches "Portuguese Water Dog", the post associated with the taxonomy term "Portuguese Water Dog" that has the types custom field "Showcase Dog" checked, should show up.

If there are multiple dog breeds being represented in the custom search archive view, I was expecting the shortcode to print multiple values (ie. irish-setter, mastiff, portuguese-water-dog), therefore invalidating any appropriates values for the wpvbreed attribute, forcing in the "no values found" section of the view.

This is what my shortcode is looking like:

[wpv-view name="ultimate-showcase-dog" wpvbreed="[wpv-post-taxonomy type='breed' format='slug']"]

However, it's only printing a single taxonomy term that is not relative to the search results. It seems to be stuck on "portuguese-water-dog". If I search "Mastiff", as seen in the screenshot, the Portuguese Water Dog still shows up. As there are no Mastiffs that have the "Showcase Dog" field checked, no results should be displayed.

Even if I remove the showcase view, it still prints "portuguese-water-dog", regardless of the results presented in the custom search archive view.

I've found an alternative method to display the results of the view based on search terms such as this:

[wpv-view name="ultimate-showcase-dog" wpvbreed="[wpv-search-term param='wpv_post_search']"]

But this forces the expectation that the user will spell the dog breed correctly, AND type the full breed name out. Easy enough for "Mastiff" or "Irish Setter", but not so much for other dog breeds such as "Xoloitzcuintli".

As this is a production site, I have the shortcode configured in this manner, to at least present a semblance of functionality.

I expect that I'm doing something wrong, or perhaps utilizing the wpv-post-taxonomy shortcode in a way that it wasn't meant to be used, so I wondering if I could get some help with this. I do hope that my explanation is clear enough.

You can view the archive page in question here: hidden link

I'd like to provide you guys access to the site, to better assist me, but the "I need help" selection doesn't immediately provide those fields for me.

#1137482

Hi,

Thanks for the details.

Yes, it is possible with some custom codes, you can create a custom shortcode to turn term's slug into term's IDs, then pass it to a taxonomy view. Here are detail steps:

1) Create a taxonomy view "Searching breed term", query terms of taxonomy "breed", filter by:
Taxonomy term ID is set by the shortcode attribute "terms"
in view's loop display the term information, for example:
[wpv-taxonomy-link]

2) Add below codes into your theme file "functions.php":

add_shortcode('term_slug_to_ids', function($atts, $content){
	$content = do_shortcode($content);
	$terms = array();
	$term_slug_array = explode(',', $content);
	if(!empty($term_slug_array)){
		$terms = get_terms( array(
			'taxonomy' => 'breed',
			'hide_empty' => false,
			'slug' 	=> $term_slug_array,
			'fields' => 'ids'
		) );
	}
	$res = '';
	if($terms){
		$res = implode(',', $terms);
	}
	return $res;
});

Please replace "breed" with your custom taxonomy slug.

3) Dashboard-> Toolset-> Settings->Front-end Content
option "Third-party shortcode arguments", add shortcode name: term_slug_to_ids

2) In the page content:
hidden link

You can get the searching breed term's slugs with shortcode:
[wpv-search-term param="wpv-breed"]

Wrap it into the new custom shortcode, turn the slugs into IDs:
[term_slug_to_ids][wpv-search-term param="wpv-breed"][/term_slug_to_ids]

Pass it to the taxonomy view, like this:

[wpv-view name="searching-breed-term" terms='[term_slug_to_ids][wpv-search-term param="wpv-breed"][/term_slug_to_ids]']

and test again.

More help:
https://developer.wordpress.org/reference/functions/get_terms/
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-search-term

#1137630
screenshot.jpg

It doesn't seem to be working just right.

A couple things: It's printing every single taxonomy term under the taxonomy "Breed" that has a post associated with it. It should only show taxonomy terms of posts that appear in the view below. On top of that, inserting your complete shortcode into my wpv-view shortcode breaks it, printing nothing but a couple brackets and quotation marks.

As far as I can tell, I've followed your directions to a tee.

I've reverted my changes, to the archive layout, as this a live production site.

#1138011

Above solution works fine in my localhost.

Please make sure you have done the step 3) as I mentioned above:
3) Dashboard-> Toolset-> Settings->Front-end Content
option "Third-party shortcode arguments", add shortcode name: term_slug_to_ids

If the problem still persists, please provide a test site with the same problem, I can setup a demo for you.

#1138897

I can download the files, trying to debug it in my localhost, will update here if there is any found.

And I have make your reply as private for security reason:
https://toolset.com/forums/topic/trying-to-pull-taxonomy-terms-outside-of-a-view/#post-1138198

#1138902
dog-breed.JPG

Here are what I found:
1) Please upgrade your DIVI theme to the latest version, according to thread:
https://toolset.com/forums/topic/divi-3-17-3-removed-auto-paragraphs-from-wpv-post-body/

There might be some problem to use old version of DIVI theme

2) Change the PHP codes as below:

add_shortcode('term_slug_to_ids', function($atts, $content){
    $content = wpv_do_shortcode($content);
    $terms = array();
    $term_slug_array = explode(',', $content);
    if(!empty($term_slug_array)){
        $terms = get_terms( array(
            'taxonomy' => 'breed',
            'hide_empty' => false,
            'slug'  => $term_slug_array,
            'fields' => 'ids'
        ) );
    }
    $res = '';
    if($terms){
        $res = implode(',', $terms);
    }
    return $res;
});

There is only one change on line 2: wpv_do_shortcode

3) Dashboard-> Toolset-> Settings->Front-end Content
option "Third-party shortcode arguments", add shortcode name: term_slug_to_ids

4) Create a taxonomy view "Searching breed term", query terms of taxonomy "breed", filter by:
Taxonomy term ID is set by the shortcode attribute "terms"
in view's loop display the term information, for example:
[wpv-taxonomy-title]

5) Display above view with shortcode:
[wpv-view name="searching-breed-term" terms='[term_slug_to_ids][wpv-search-term param="wpv-breed"][/term_slug_to_ids]']

I am not sure where do you want to display it, to test the result, you can put it into view "Search Results", section "Loop Editor"

It works fine, see screenshot dog-breed.JPG

#1139007

There's seems to be a slight misunderstanding in what I need. Your shortcode does work if filter options are selected, as in your example. This isn't objectionable in the very slightest, as I do need this to work with the filters as well, however it doesn't work if a user utilizes the text search (parameter "wpv_post_search"). On top of that, it conflicts with the context in which I need these breed terms printed.

I need the values printed inside a views shortcode. Specifically for the showcase dog breed, as seen below:

[wpv-view name="ultimate-showcase-dog" wpvbreed="[wpv-view name='searching-breed-term' terms='[term_slug_to_ids][wpv-search-term param='wpv-breed'][/term_slug_to_ids]']"]

This section appears just below the navigation and search bar. If you paste the above code in your layout, you'll see just how it's breaking. Please refer to my screenshot from reply #1137630

#1140153
Masti.JPG

Sorry for the misunderstandings, you are right, there isn't such kind of built-in feature within Views plugin, here is a workaround by another post view, here is the detail steps:
1) you can create second post view "Search Resuls Breed Terms", for example: Dashboard-> Toolset-> Views, find and duplicate the view "Search Results"
- query "Dogs" posts with the same filters as view "Search Results"
- and disable pagination, display unlimited posts,
- enable option "Disable the wrapping DIV around the View"
- in the view's loop, display breeds taxonomy shortcodes, like this:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-item index=1][wpv-post-taxonomy type="breed" format="name"]##[wpv-post-taxonomy type="breed"]
		[wpv-item index=other]|[wpv-post-taxonomy type="breed" format="name"]##[wpv-post-taxonomy type="breed"]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		[wpml-string context="wpv-views"]No item found[/wpml-string]
	[/wpv-no-items-found]
[wpv-layout-end]

No other custom HTML div tags

2) But it will output duplicated term items, so you will need to setup a custom shortcode to remove the duplicated term items:

add_shortcode('display_unique_terms', function($atts, $content){
    $content = wpv_do_shortcode($content);
    $terms = array();
    $term_info_array = explode('|', $content);
    foreach($term_info_array as $term_info){
		$term_item_info_array = explode('##', $term_info);
		$terms[trim($term_item_info_array[0])] = $term_item_info_array[1];
	}
	$terms = array_unique($terms);
	ksort($terms);
    $res = '';
    if($terms){
        $res = implode(',', $terms);
    }
    return $res;
});

3) put above post view into view "Search Results", section "Loop Editor", like this:

[display_unique_terms][wpv-view name="search-resuls-breed-terms" view_display="layout" cached="off"][/display_unique_terms]

Here we output only the result with attribute view_display="layout".

Test it in front-end:
Masti.JPG

search with keyword "Masti", it does output the term "Mastiff" only.

#1140534
screenshot.jpg

Printed results are favorable, based on searches and filters, and still seems to work as intended after I altered it to remove the anchors on the values:

[wpv-item index=1][wpv-post-taxonomy type="breed" format="name"]##[wpv-post-taxonomy type="breed" format="name"]
[wpv-item index=other]|[wpv-post-taxonomy type="breed" format="name"]##[wpv-post-taxonomy type="breed" format="name"]

However it's still breaking my [wpv-view name="ultimate-showcase-dog" wpvbreed=""] views shortcode when I use it to supply the value for my "wpvbreed" query filter shortcode attribute. I even registered it under the "Third-party shortcode arguments" settings.

This is what I have in the layout's cell:

[wpv-view name="ultimate-showcase-dog" wpvbreed="[display_unique_terms][wpv-view name='search-results-breed-terms' cached='off'][/display_unique_terms]"]

Note that I removed the "view-display='layout'" attribute so that the view would only display the value, and not the corresponding "wpv-view-layout" div, I fixed your typo for the spelling of "results", and removed the content wrapped by the [wpv-no-items-found] shortcodes in the view, so that nothing will print if no results were found.

Please review the screenshot.

#1140973
portu.JPG

Thanks for the clarification, and above workaround is only a demo for display all term's link related with the search result.

You are right, there is a typo, but it is only an example, you just need to make sure the view's slug is correct:

I assume you are using post view "Search Results Breed Terms", please edit it, make sure it is using slug "search-results-breed-terms", you can see it just below the view title.

You can output the result directly with above view.

1) Modify the post view "Search Results Breed Terms", in section "Loop Editor", copy/paste the same codes as your post view "Ultimate Showcase Dog":

[wpv-layout-start]
	[wpv-items-found]
    <!-- wpv-loop-start -->
        <wpv-loop>
          	[wpv-post-body view_template="Ultimate Showcase Loop Item"]
        </wpv-loop>
    <!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
	<div id="showcase-invite">
		<h2>Showcase Your Dog Listing</h2>
        <div class="subheader">Have your dog's picture showcase your specific breed</div>
        <div class="btn-container"><a href="/featured" class="green-btn">read more</a></div>
	</div>
	[/wpv-no-items-found]
[wpv-layout-end]

And make sure these:
- it is using same filters as post view "Search Results",
- display only 1 item

2) Edit the layout "Layout for Search Results", in section "Row 3", change the visual editor cell codes as below:

  	[wpv-view name="search-results-breed-terms"]

We use the new post view "Search Results Breed Terms" to replace post view "Ultimate Showcase Dog"

Test it in front-end:
for example search keyword "portu", it does output a featured post in the header, see screenshot portu.JPG

Hope it is clear.

#1141446

It looks like it is working correctly. Ran a few tests, and it seems to be operating in a manner that is favorable. Thank you for your help.