Skip Navigation

[Closed] Conditional Output within the Loop Output Editor or Query Filter help

This support ticket is created 7 years, 5 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
- 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+01:00)

Tagged: 

This topic contains 42 replies, has 3 voices.

Last updated by Nigel 7 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#454700

I have a 'Product Ad' Post Type that displays on a CONPM Content Template. Among other things, this template displays a CONLIST View of info pulled from a 'Supplier Details' Post Type using Fields from within the Loop Output Editor (2-column table-based-grid). That part works fine.

Within that Loop however, I need to use a 2-part conditional statement (If/And) to filter displayed results. I can't figure out how to write either part of the conditionsl statement with respect to knowing the proper [wpv-field] that calls from other Post Types or shared Taxonomies.

Example:
The first conditional statement within the Loop needs to check if the [wpv-post-taxonomy type='product-list' format='name'] is equal to the [wpv-post-title] of the current page 'Product Ad' that the Loop displays results on (as opposed to any query item within the Loop).

AND

The second conditional statement within the Loop needs to check if the [wpv-post-taxonomy type='indiana-county' format='name'] is equal to the same taxonomy within a 'Contractor Details' Post Type outside of the Loop.

I'm trying to do this as a Conditional Output within the Loop Output Editor. I also tried the Query Filter arguments but not able to find the proper Fields.

#454830

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The first conditional statement within the Loop needs to check if the [wpv-post-taxonomy type='product-list' format='name'] is equal to the [wpv-post-title] of the current page 'Product Ad' that the Loop displays results on (as opposed to any query item within the Loop).
=======>
Well - you can get current page title using shortcode:

[wpv-post-title id='$current_page']

But,
There are some known issues when you use "$current_page" with ajax view and the good news is that you can follow the following hotfix to resolve that and once you apply the patch given with the following URL, $current_page will work fine.
=> https://toolset.com/errata/shortcode-displays-info-view-loop-item-instead-current-page-manual-ajax-pagination/

More info (Doc):
https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/

The second conditional statement within the Loop needs to check if the [wpv-post-taxonomy type='indiana-county' format='name'] is equal to the same taxonomy within a 'Contractor Details' Post Type outside of the Loop.
=======-=>
You can dispaly the taxonomy conditoinally using [wpv-conditional] shortcode:

More info:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/displaying-taxonomies-conditionally/

If this is not the case with your 2nd issue, I would like to have test case example to understand your issue.

#455168

Regarding the 1st conditional statement above. I tried the following:

[wpv-conditional if="( '[wpv-post-taxonomy type='product-list' format='name']' eq '[wpv-post-title id='$current_page']' )"][wpv-post-body view_template="Loop item in CON List of Local SUPs Selling Product"][/wpv-conditional]

It worked IF the taxonomy had ONLY the correct term checked so that it was equal to the $current_page. That said, the conditional statement must continue working even if there are other random terms checked ... of which it does not (as not being eq anymore). I tried switching the eq to >= without good results.

So was thinking that I really just need to check if my specific term is checked based upon what page this is all displaying on. So I tried this (after registering 'has_term' and using your 'hotfix' regarding the .php file):

[wpv-conditional if="( has_term('A.M.I. Services', 'product-list', null) eq '1' )" ]This Test Works [/wpv-conditional]

This works fine as a test within the Loop. However, when I changed out the term name so that it auto calls current page, it failed to work (nothing showed).

[wpv-conditional if="( has_term('[wpv-post-title id='$current_page']', 'product-list' null) eq '1' )" ] This Not Worked regardless which/how many terms checked. [/wpv-conditional]

Note that the [wpv-post-title id='$current_page'] shortcode works as a test both inside and outside the Loop otherwise by itself. Thanks.

#455861

Nigel
Supporter

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

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

Minesh is on vacation this week and so I have just re-assigned this ticket to myself, and I will read through it and get back to you with an update in the morning.

Thanks for your patience.

#456174

Nigel
Supporter

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

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

As you are already using a built-in WordPress function in your example—has_term—you might want to try using a built-in WordPress function to retrieve the page title, namely single_post_title (https://codex.wordpress.org/Function_Reference/single_post_title).

So you could use has_term( single_post_title( '', false ) ).

If that doesn't work you might want to set that up as a custom shortcode.

Let me know how you get on and if you need help with that.

#456184

I still need help with syntax. This statement works as I want it to:

[wpv-conditional if="( has_term('A.M.I. Services', 'product-list', null) eq '1' )" ][wpv-post-body view_template="Loop item in CON List of Local SUPs Selling Product"][/wpv-conditional]

The problem is that I need to remove the A.M.I Services variable and replace that code with something else that represents the current page of where the Loop is displaying on.

I tried this:
[wpv-conditional if="( has_term('[wpv-post-title id='$current_page']', 'product-list' null) eq '1' )" ]

And tried this (not sure of syntax):
[wpv-conditional if="( has_term( single_post_title( '', false ) ), 'product-list' null) eq '1' )" ]

I'm on an unknown product page which needs to list all suppliers that sell that product (cooresponding to the page being displayed). Of course this is just the first condition.

I then need to filter that list to the suppliers who deliver to a specific region (which I'd need help on too, when can solve first condition. Thanks.

#456854

Nigel
Supporter

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

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

Re-reading your initial post I am unclear about what exactly you are trying to achieve, but it seems to me that you might be better served setting this up with query filters (which will limit what posts are returned by the view) rather than conditional shortcodes (which manipulate what is output by the view as it loops across the returned posts).

Sorry for the inconvenience, but can we go over what you are doing to be clear on that so I can determine what is your best option?

From your question I see that you have

- a custom post type "Product Ad"
- a custom post type "Supplier Details"
- a custom post type "Contractor Details"
- a custom taxonomy product-list (the terms for these have the same slug as the Product Ad posts?)
- a custom taxonomy indiana-county

I don't see how these are connected, i.e. whether you have any post relationships set up, or what taxonomies apply to what post type.

Can you elaborate on what content you want on the page.

"The page" we are talking about is, I think, a single Product Ad post, for which there exists a content template. Please describe what else you are adding to this page.

When I have a clearer picture I should be able to help you set this up in the most efficient way.

#456975

I have a WordPress Multisite setup using subdirectories.
On particular contractor website, I have the following Post Types:
1. Contractor Details (i.e. contact info, service locations taxonomy)
2. Manufacturer Details (i.e. contact info, 42 Product Ads Post Types)
3. Supplier Details (i.e. contact info, 3 Supplier Details Post Type, Product Lists Taxonomy showing which manufacturer products the supplier sells)

Both the above 'Contractor Details' and the 'Supplier Details' Post Types use common Taxonomies (53 State/County) to show geo regions that each services.

On the same particular contractor website, I also have a 'Product Ad' Post Type which I need assistance on.
It displays on 'CON PM Master Raw PM Ad' Content Template.
Each 'Product Ad' page displays info on a specific product. [OK]
That same Content Template also displays 3 Views:
1. 'PM Master Ad Author' View (i.e. About the Manufacturer) [OK]
2. 'con-list-of-local-sups-selling-product' View
(i.e. list of all suppliers ... [NOT OK-HELP NEEDED W/ FILTERS BELOW]
... filtered to all suppliers that sell that specific product ...
AND ... filtered to all suppliers within the same Geo Region as the Contractor who installs it (i.e. cooresponding geo region taxonomies between 'Suppliers Details' and 'Contractor Details' Post Types))
3. 'MoreProdPerCatView' View (i.e.Similar Products) [OK]

#457348

Nigel
Supporter

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

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

Screen Shot 2016-11-15 at 17.05.49.png

Thanks for the clarification.

So the View we are concerned with lists suppliers, and it appears on a single Product Ad page.

It needs a filter to only display suppliers with a 'product-list' taxonomy term equal to the name of the product currently displayed.

We can do that by passing the title of the current page to the view with a shortcode attribute.

I created a view "suppliers-of-a-product". I added a query filter that is a taxonomy filter where the taxonomy term is set by the shortcode attribute 'wpvproductlist'. (The attribute name was auto-generated based upon my taxonomy slug. See screenshot.)

Now, in my products content template where I insert the supplier view I add the post title of the current page as the wpvproductlist shortcode attribute like so:

[wpv-view name="suppliers-of-a-product" wpvproductlist="[wpv-post-title]"]

The second filter is more potentially problematic.

You are trying to add a filter to only display suppliers where the region taxonomy is determined by... what exactly? We were able to specify what product we were interested in for the first filter because we are on a product page, so we can meaningfully query for "suppliers who supply this product".

But how are we to know what contractor details we want to take the region taxonomy term from? I'm missing something here.

#457772

I got the first filter to work more or less as you suggested (perhaps check). I created an 'Available Suppliers' View. I set the Content Selection to our Supplier Details Post Type. Set the Taxonomy Query Filter 'Product Lists slug in one of those set by the View shortcode attribute wpvproductlist'. Because I just didn't want our supplier names to appear by themselves, I placed [wpv-post-body view_template="Loop item in Available Suppliers"] in the Loop Output Editor. I then placed additional 'Supplier Details' Fields in the 'Loop Item along with some CSS in Available Suppliers'. Within our Content Template we simply call [wpv-view name="available-suppliers" wpvproductlist="[wpv-post-title]"].

Do you have a help page on the 'Product Lists slug in one of those set by the View shortcode attribute wpvproductlist' query filter as I really don't understand how/why this works for future reference?

Our second filter:
Now that the above is finding all suppliers who sell a particular 'Product Ad' based upon a 'Product List' Taxonomy within each 'Supplier Details' Post Type ... we now want to further filter our result down to only those suppliers selling in a particular geographical region that cooresponds to ONE contractors service area.

Thus for the supplier:
The same 'Supplier Details' Post Type from the first filter has 53 other Taxonomies (each with about 100 different field names) that we need to test.

For the contractor:
Because we are within a contractors website placing this code, there is only ONE 'Contractor Details' Post Type. It uses the same 53 geo region Taxonomies (Indiana, Illinois, etc.) as that of the suppliers.

#458083

Nigel
Supporter

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

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

The documentation for passing arguments to the view to use in query filters is here: https://toolset.com/documentation/user-guides/passing-arguments-to-views/

In our case we wanted our view to only return suppliers that matched a particular taxonomy term. We got that taxonomy term by passing it to the view via a shortcode attribute.

So, on a single product page we add a view that lists suppliers of that product and we also want to restrict the list to suppliers in a geographic area matching the area covered by a contractor.

But which contractor?

I see from your description that the suppliers and contractors both use the same region taxonomy, but how do I know which contractor to get the region from?

#458091

We're on a WordPress Multisite with hundreds of individual sites using a sub-directory setup (as opposed to a sub-domain setup). Those sites have three groupings (manufacturers, suppliers and contractors).

Upon any 'Contractor' site there may be MANY manufacturers, products, and suppliers Post Types. However, there is only ONE contractor data-set upon there own contractor site (i.e. Smith Builders). Thus, there is only one 'Contractors Details' Post Type to fetch Taxonomy data from (regardless of how many different 'Product Ads' there are).

So I imagine we're pullng from the ONLY 'Contractors Details' Post Type of the URL (i.e. hidden link)?? We don't need to identify WHICH contractor as there is only one; instead we need to simply compare 53 different taxonomies of the only 'Contractor Detials' Post Type. (sorry if not understanding or explaining well).

#458340

Nigel
Supporter

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

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

Screen Shot 2016-11-18 at 13.29.21.png

OK, I have it now.

We need to employ the same trick as before, adding a taxonomy filter for the region to the view and then telling the view which region by passing the required region as a shortcode parameter.

So my view will have two taxonomy filters as per the screenshot (in my test site my region-based taxonomy is called 'location').

In my content template for products where I add the suppliers view I pass two shortcode attributes, one the product (which comes from the title of the page we are on) and the second one the location, which might look something like this if we were hard-coding the location:

[wpv-view name="suppliers-of-a-product" wpvproductlist="[wpv-post-title]" wpvlocation="california"]

Now, there isn't a built-in Toolset shortcode that can retrieve the location taxonomy of the only contractor post on this site (which is where I have understood we will get the contractor region from).

So we need a custom shortcode to retrieve that and add it as our shortcode attribute.

Here is the code I put together for such a shortcode. Note that you may have to modify it for the slugs for your taxonomy etc.

/**
 * Location taxonomy term of only contractor post
 */
add_shortcode( 'contractor-location', function(){

	$args = array(
		'post_type'		=>	'contractor',
		'post_status'	=>	'published'
	);
	$contractors = get_posts( $args );

	$terms = get_the_terms( $contractors[0], 'location' );

	if ( $terms && !is_wp_error( $terms ) ) {

		$locations = array();

		foreach ($terms as $term) {
			$locations[] = $term->name;
		}
		$locations_list = join( ', ', $locations );
	}
	return $locations_list;
});

To be able to use this shortcode as an argument in a views shortcode you first need to register it in the settings page at Toolset > Settings > Front End Content, and enter the name of the shortcode, 'contractor-location'.

Now update the shortcode where you insert your view in the product content template to add the shortcode attribute for the location like so:

[wpv-view name="suppliers-of-a-product" wpvproductlist="[wpv-post-title]" wpvlocation="[contractor-location]"]

I tested that out myself and it appeared to work fine. Please note, though, that writing custom code like this goes beyond our support policy, so if you find you have problems implementing it I can't debug it for you and you may need to call in a developer to help.

Good luck, let me know how you get on.

#459962
toolset.PNG

White-screen of death. I too have been trying to find a developer. I went through your site of experts. Most don't respond. Others don't want a small job. LinkedIn people either know nothing about Toolset or want to rewrite and custom code all. It's been over TWO weeks and am ready to lose my job over this. I got a recent email rant from your founder complaining of having to wait over the weekend with WordPress Plugins and how those couple days effected your business. I realize I'm not a coder. But your marketing says Types can capture any data (it has); and Views can display it (even taxonomies) w/o coding (it isn't). I paid for a premium plugin and multiple premium support and get frustrated when having to sit on my hands for weeks and then told to go hire somone. Certainly there must be a better support system. WHO can I hire ? </end rant>
-----
A) As an alternative, as you earlier said, "there isn't a built-in Toolset shortcode that can retrieve the location taxonomy of the only contractor post on this site" created by Types, (which to me is amazing), So I created a con-serve-indiana View so that I'd have a shortcode to call.

I put this in the Content Template w/o much luck:

[wpv-view name="available-suppliers" wpvproductlist="[wpv-post-title]" wpvindianacounty="[wpv-view name="con-serve-indiana"]"]

I tested the shortcodes by placing in the Loop and both below output correctly.
[wpv-post-taxonomy type='indiana-county' format='name'] where suppliers deliver to. And [wpv-view name="con-serve-indiana"] for contractor service areas. See screenshot for filters.
-----
B) Your latest option which gave me white-screen. Perhaps you can find my error. I have this in Content Template:

[wpv-view name="available-suppliers" wpvproductlist="[wpv-post-title]" wpvindianacounty="[con-indiana]"]

And I have this as my code in my custom_functions.php file:

add_shortcode( 'con-indiana', function(){
 
    $args = array(
        'post_type'     =>   'contractor-detail',
        'post_status'   =>   'published'
    );
    $contractor-details = get_posts( $args );
 
    $terms = get_the_terms( $contractor-details[0], 'indiana-county' );
 
    if ( $terms && !is_wp_error( $terms ) ) {
 
        $locations = array();
 
        foreach ($terms as $term) {
            $locations[] = $term->name;
        }
        $locations_list = join( ', ', $locations );
    }
    return $locations_list;
});
#459979

Nigel
Supporter

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

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

I'll review this in the morning when I'm back at work, but I realise we are in different timezones and it might help you to know that a quick glance at the code you pasted reveals the source of the white screen.

You have used variable names with a hyphen, which PHP treats as a minus sign. Try using underscores, i.e. $contractor_details instead of $contractor-details.

The topic ‘[Closed] Conditional Output within the Loop Output Editor or Query Filter help’ is closed to new replies.