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.

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
- 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
#463031

I sent an earlier reply to your email. Additionally, there seems to be another problem. When i add multiple Query Filters it does not display correctly. It appears I need conditional statements like this:

(wpvproductlist AND (wpvindianacounty OR wpvillinoiscounty OR wpvohiocounty))

#463211

Nigel
Supporter

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

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

I received your email with the follow-up questions, but overlooked replying to it later. Sorry about that, it's one of the perils of communicating off-forum, because I have a queue of forum tickets to attend to which is how I can see what I have outstanding that needs replying to.

If you have something sensitive to share with me you can drop me an email, but it would be useful to post a brief comment in the forum so that it appears in my list of ongoing tickets needing a reply.

So, to the questions themselves. I'll come back to the issue of the CSS styling, let's get the filters working first.

There are two main issues here.

One is that the custom shortcode I coded was setup to work with one region taxonomy (Indiana States), and now that you have it working you want to be able to use replicate it to work with 50+ taxonomies. (Note that you register the shortcodes to be able to use them inside Views shortcodes, as you do when you use them as a shortcode attribute inside your wpv-view shortcode. You don't need to register them as functions for conditional shortcodes.)

The second, reported here in your previous post, is that the taxonomy filter needs to be of the form product AND (region1 OR region2 OR region3 etc.), but when you add the filters to the view you only have the options of AND all taxonomy filters or OR any of the taxonomy filters.

This last point is a limitation of Views, you cannot construct more complex taxonomy queries, although it is something WordPress is capable of: see Nested Taxonomy Handling under https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

To get round this there are 2 choices. One is to change the product taxonomy to use custom fields instead. You can then have a filter for the product as a custom field, and then all of your region taxonomy filters with an OR relation.

If changing the product taxonomy to a custom field is unappealing then we are left with using the Views API and manually coding the taxonomy query to rewrite it in the required more complex form.

From the debug info, here we can see the tax_query part of the relevant query when we visit the AMI product page, for example:

[tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => alabama-county
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 432
                        )
                    [operator] => IN
                    [include_children] => 1
                )
            [1] => Array
                (
                    [taxonomy] => indiana-county
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 366
                            [1] => 1089
                            [2] => 1203
                            [3] => 1330
                            [4] => 1880
                            [5] => 1898
                            [6] => 2161
                            [7] => 2437
                            [8] => 3065
                            [9] => 3317
                        )
                    [operator] => IN
                    [include_children] => 1
                )
            [2] => Array
                (
                    [taxonomy] => new-mexico-county
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 522
                        )
                    [operator] => IN
                    [include_children] => 1
                )
            [3] => Array
                (
                    [taxonomy] => product-list
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 3656
                        )
                    [operator] => IN
                    [include_children] => 1
                )
            [relation] => AND
        )

We would need to modify that tax_query to bundle all the non-product-list taxonomies into a nested array with an OR relation.

I am going to work on that now and see if I can come up with something.

Also, with the custom shortcode I wrote, it specifically checks for the indiana-county taxonomy.

Rather than register 50+ custom shortcodes I can modify it so that you pass the taxonomy you are using as a shortcode attribute, so you can re-use the same custom shortcode for all of the region taxonomies.

Shall I do that, too?

#463218

Nigel
Supporter

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

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

I quickly took a look at modifying the shortcode, and I think you could use something like this:

add_shortcode( 'con_region', function( $atts = [] ){
    
    // expects one attribute, region, which is the region taxonomy slug
    // e.g. [con_region region="indiana-county"]

    $args = array(
        'post_type'     =>   'contractor-detail',
        'post_status'   =>   'published'
    );
    $contractor_details = get_posts( $args );
     
    $terms = get_the_terms( $contractor_details[0], $atts['region'] );
     
    if ( $terms && !is_wp_error( $terms ) ) {
    
        $locations = array();
    
        foreach ($terms as $term) {
            $locations[] = $term->name;
        }
        $locations_list = join( ', ', $locations );
    }
    return $locations_list;
});

Where you use the custom shortcode(s) to pass the location, instead of using [con_indiana], [con_alabama] etc., you would use [con_region region="indiana-counties"], [con_region region="alabama-counties"] etc.

#463284

1. I already have all 53 shortcodes registered via Settings >> 'Third-party shortcode arguments', so no need to write code.

2. Regarding the usage of mixed conditional statements for the taxonomies. The option of swithching the Product Tax to Custom Fields shouldn't be considered. The better choice, so not affecting the entire multisite, would be the Views API. Although I would need your valued assistance.

3. I too, have already copied, modified and in-serted all 53 PHP arguments as individuals within custom_functions.php file for the State tax's.

4. I turned back on the WP-debug and the Toolset debug mode.

#463313

Nigel
Supporter

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

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

I'm done for the week but I knocked together some code so that you can try it out. I'd love to think it will work first time, but I haven't been able to test it on your site, I have tested it with some dummy data which was generated from your site, so fingers crossed.

I should point out that it may be quite brittle, i.e. I am working on the format of the query generated as it is currently. Editing/adding/removing the query filters could modify that format and break it.

Before I copy the code, let me show you what it aims to do. You will need to edit the view id so that it only affects the required view. It is going to take the existing taxonomy filters which are all related with AND and split it into product AND (location1 OR location2 OR location3...) etc.

So the tax_query currently generated looks like this as generated from an example page:

[tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => alabama-county
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 432
                        )

                    [operator] => IN
                    [include_children] => 1
                )

            [1] => Array
                (
                    [taxonomy] => indiana-county
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 366
                            [1] => 1089
                            [2] => 1203
                            [3] => 1330
                            [4] => 1880
                            [5] => 1898
                            [6] => 2161
                            [7] => 2437
                            [8] => 3065
                            [9] => 3317
                        )

                    [operator] => IN
                    [include_children] => 1
                )

            [2] => Array
                (
                    [taxonomy] => new-mexico-county
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 522
                        )

                    [operator] => IN
                    [include_children] => 1
                )

            [3] => Array
                (
                    [taxonomy] => product-list
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 3656
                        )

                    [operator] => IN
                    [include_children] => 1
                )

            [relation] => AND
        )

I need to take that and transform it into something that looks more like this:

[tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => product-list
                    [field] => id
                    [terms] => Array
                        (
                            [0] => 3656
                        )
                    [operator] => IN
                    [include_children] => 1
                )
            [1] => Array
                (
                [0] => Array
                    (
                        [taxonomy] => alabama-county
                        [field] => id
                        [terms] => Array
                            (
                                [0] => 432
                            )
                        [operator] => IN
                        [include_children] => 1
                    )
                [1] => Array
                    (
                        [taxonomy] => indiana-county
                        [field] => id
                        [terms] => Array
                            (
                                [0] => 366
                                [1] => 1089
                                [2] => 1203
                                [3] => 1330
                                [4] => 1880
                                [5] => 1898
                                [6] => 2161
                                [7] => 2437
                                [8] => 3065
                                [9] => 3317
                            )
                        [operator] => IN
                        [include_children] => 1
                    )
                [2] => Array
                    (
                        [taxonomy] => new-mexico-county
                        [field] => id
                        [terms] => Array
                            (
                                [0] => 522
                            )

                        [operator] => IN
                        [include_children] => 1
                    )


                [relation] => OR
                )

            [relation] => AND
        )

Using that as test data I got it to work with the following code:

/** 
 * Modify taxonomy query to have 
 * product AND (location1 OR location2 OR location3...) type tax_query
 */
add_filter( 'wpv_filter_query', 'custom_tax_query', 101, 3);
function custom_tax_query( $view_args, $view_settings, $view_id ){

	if ( $view_id == 99 ) { // edit this for the view id

		$taxquery = $view_args['tax_query'];

		// The product list taxonomy argument is the penultimate array entry: strip it
		$productlist = array_splice($taxquery, -2, 1 );

		// Change the location tax query comparison to OR
		$tax['relation'] = 'OR';

		// Add the location tax query as nested array to productlist
		$productlist[] = $taxquery;

		// Define relation between product and location taxonomy queries as AND
		$productlist['relation'] = 'AND';

		$view_args['tax_query'] = $productlist;
	}

	return $view_args;
}

Suck it and see.

If it doesn't work, try scattering some error logging in the code to see what that reveals, but in any case, let me know how you get on.

#464764

Are you for hire? If not, whom?

#464988

Nigel
Supporter

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

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

I don't have any particular recommendations, I'm afraid, but I would like to help you get this working.

Do you have any feedback about the last update?

Let's proceed like this.

Add the following two plugins to your site:
https://wordpress.org/plugins/my-custom-functions/
https://wordpress.org/plugins/wp-php-console/

The first one of these lets you (or me) add PHP code without the need to edit your functions.php file. So move the custom code I have given you for the shortcode and for the custom taxonomy query from your functions.php file and add it using this plugin.

The second plugin let's me send messages to the debug logs and view them in the browser, without requiring FTP access to view your error logs.

Together they should help me add/edit code to your site and debug it, without needing anything more than admin access.

Please give me some specifics about what happened with the custom taxonomy query so I know what to look for and what needs fixing.

#465996

I put in your two suggested plugins. I moved code from my custom_functions.php into My Custom Functions. Regarding your new comment/code above, I had got really confused so I backed it out.

Another problem. I upgraded Views pluging and my theme and it seem for some reason the section immediately above and below what we've been working on, on the AMI Product Page is now not showing up. This error is consistant on all the multisites whereas it had been working for months. The 'about the manufacturer' and the 'similar products' sections. They both are using a Querry Filter 'Value set by the current post in the loop'.

All three of these errors are on each of the Product Page(s) as we've been discussing.

#466594

Nigel
Supporter

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

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

Sorry, that plugin My Custom Functions I now realise doesn't work on multi-site.

Could you replace it with Code Snippets instead, which does if you install it across the network: https://wordpress.org/plugins/code-snippets/

Regarding the missing content, did that happen independently of you moving the code from your functions.php file, or did you do that at the same time as updating Views and WordPress?

#466640

Morning Nigel: I exchanged My Custom Functions with Code Snippets. I placed code in a 'test' snippet and activated it.

    Regarding the missing content, did that happen independently of you moving the code from your functions.php file, or did you do that at the same time as updating Views and WordPress?

The missing content relating to the use of an existing Query Filter happened after Views update and prior to installing the previous two suggested plugins. This filter has stopped working across ALL sites in network, whereas nobody was in the backend of any of those sites.

#469496

I responded same day to your Dec. 15 private email.

#469504

Nigel
Supporter

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

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

Hi Tom

I'm sorry I haven't been back in touch earlier but as this thread relates to a solution requiring custom coding (which falls outside our support policy) I can only work on it when I have some free time, which I simply haven't had.

It is very much dependent on the general support workload, but I will prioritise it the moment I have a gap where I can work on it.

#478900

Nigel
Supporter

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

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

Hi Tom

Over the past few weeks I have made several abortive attempts to work on this.

Each time after reminding myself of what's required I try to set up a working environment where I can add and edit the custom code, and, crucially, am able to debug the code.

I have tried various combinations of plugins which should work in your multi-site set up, but I haven't succeeded in getting any to work, and, please believe me, it is not for want of trying.

I'm also wary that it is a very brittle set up for testing code. Even the simplest typo in any code I add can create a 500 error that I have no means to recover from, given that I don't have FTP access.

I appreciate you don't want to let me make a local copy of the site for testing, but I'm afraid without it I've reached something of a brick wall and there is not much more I can do as things are.

Which means that I am back to recommending you contact a developer to finish the implementation of this, although I know you haven't had much luck with that.

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