Skip Navigation

[Resolved] Question about query filters

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/Karachi (GMT+05:00)

This topic contains 12 replies, has 3 voices.

Last updated by Waqar 1 year, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2636609

We have a view with a filter that allows us to filter on a taxonomy. The page is our Schedule page that shows all Matches. The taxonomy filter is on a taxonomy called Tournaments. That page is here:

hidden link

Currently the page shows ALL match results and then you can apply the filter to limit the results to each specific tournament.

What we would like to do instead is to have this page default to showing only the current tournament and then have all the other tournament schedules accessible through the filter. So in the case of this page, assume the August 19, 2023 tournament to be the current tournament. That one is Coca-Cola Series - Atlanta. Is there any way you can think of to make the page only show the August 19, 2023 results when you go to the page initially, but still have the other tournaments available to access through the filter? I'm thinking we may have to figure out a way to designate a Current Tournament field in some way for this, but if I set up another query filter for that, then it will block the display of the other tournaments when people select those because those won't have that Current Tournament designation. We're only wanting that designation to impact the INITIAL page load display, and to not block the display after a non-current tournament is selected in the filter. Any thoughts?

#2636661

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

What we can do is we can dynamically add/apply the filter when the view loads the results first time but how to know that this is the current tournament or what tournament needs to be displayed with first page load?

#2636717

Yeah, how does the system know which tournament is the current tournament and how to make that something that can be changed easily with each new tournament? That's the big question on this thought. All we have for Tournaments are the taxonomy options. I don't know of any way to give a taxonomy option some quality that can be used by a query filter in a way that would keep it fully automated. We could always change the view manually and set which specific tournament is the current tournament at each point in time. That would be the way to go if this idea can't be fully automated.

Another way to do that same thing would be to just set the URL of the top menu item to be the full URL from after the filter on the single tournament is selected. That way that page would load with that tournament filtered on initial load. But that would require manually changing it with each tournament. That probably wouldn't be so bad.

Maybe we can set up an initial filter by date instead, which will only allow the current tournament to be displayed. If we go that route, there would have to be a way to filter to show only matches on the latest date present in the system. So using this August 19 tournament as the example, the date filter would have to recognize August 19 as the latest date of the matches in the system and keep including all matches on that date until we add matches that are on a future date. Once there is a match dated October 10, for example, then the August 19 ones would no longer display on initial page load and the October 10 ones would then be the ones that display. Is there any way to do it that way?

#2637473

Minesh
Supporter

Languages: English (English )

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

We are using grouping to display the current output within the view:
=> hidden link

[heading condition="year" value="[types field='match-date-and-time' style='text' format='F j, Y'][/types]"]
<h4 style="margin: 30px 0px 10px 0px;">[types field='match-date-and-time' style='text' format='F j, Y'][/types]</h4>
[/heading]

What is the mechanism to know what matches to display and what to exclude. The date way you told, we can take todays date, do you mean that we should exclude all posts of past dates and display the posts belongs to future dates only?

#2637519

No, that wouldn't work. What we want to display is the current tournament. But the definition of "current tournament" can also be considered the most recent tournament once it becomes a past tournament. What you describe would show only the matches on that day or in the future. What we're trying to achieve is to just show matches from the upcoming tournament OR the most recent tournament if no future matches are in the system yet. So we can't even do it by saying to show everything going 30 days into the past either because the most recent tournament would then stop displaying 31 days after it is over. We want the last tournament to keep displaying by default until the first match of the next tournament is created.

If there isn't a way you can think of to get this, then what I'll just do is to change the URL on the Schedule page in our top menu system from

hidden link

to

hidden link

for example, and then just manually change it every time we start adding new matches from the next tournament to the schedule.

#2638525

Hi,

Minesh is on vacation so I'll be following up on this ticket.

I'll need to perform some tests on my website with similar set up to suggest the next steps.

Will share the findings, as soon as this testing completes.

Thank you for your patience.

regards,
Waqar

#2639677

Thank you for waiting.

After some testing, I was able to make this default tournament selection work, using the following steps:

1. I added a new term field group 'Tournaments Fields' for the 'Tournaments' taxonomy terms, and added a checkbox type field 'Is this current'.
( screenshot: hidden link )

This will allow you to select which term should be selected as the currently 'active' one. For now, I've set the term 'Coca-Cola Series - Atlanta' as active from its edit screen.

In the future, you can uncheck this field for this term and select the other one, as needed.

2. Next, I created a taxonomy view 'View to get the current tournament term slug' that shows the terms 'Tournaments' with minimal output.

The view's loop item consists of a condition so that only the term's slug is shown where the 'Is this current' field is checked (that is the field's value is equal to '1'):


[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-conditional if="( '[types termmeta='is-this-current' output='raw'][/types]' eq '1' )"][wpv-taxonomy-slug][/wpv-conditional]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found][/wpv-no-items-found][wpv-layout-end]

As a result, this view's output will give you the slug 'coca-cola-series-atlanta' of the currently active term 'Coca-Cola Series - Atlanta'.

3. The last step is to use this slug from the view in a custom function attached to the 'wpv_filter_query' filter.
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )

You'll find the following code added in the snippet named 'tournament-schedule-default-filter', in the Toolset's custom code section:


add_filter( 'wpv_filter_query', 'filter_tournament_schedule_default', 1000 , 3 );
function filter_tournament_schedule_default( $query_args, $view_settings ) {

	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 832) ) {
		if(empty($_GET['wpv_filter_submit']))
		{
			$target_term_slug = do_shortcode('[wpv-view name="view-to-get-the-current-tournament-term-slug"]');
			$query_args['tax_query'][0]['taxonomy'] = 'tournament';
			$query_args['tax_query'][0]['field'] = 'slug';
			$query_args['tax_query'][0]['terms'][0] = $target_term_slug;
			$query_args['tax_query'][0]['operator'] = 'IN';
			$query_args['tax_query'][0]['include_children'] = '1';

			$query_args['tax_query']['relation'] = 'AND';
			$_GET['wpv-tournament']=$target_term_slug;
		}
	}

	return $query_args;
}

This function checks if the current view is 'Schedule' ( ID: 832 ) and if no search has been performed. In this case, it automatically applies the tournament term filter query, based on the active term's slug.

I hope this helps and please let me know if you need further assistance.

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/

#2639761

Hmm, I'm trying to understand the logic here. Please help me to understand how this will work.

I do see that it is working on the schedule page.

Where you said:
"This will allow you to select which term should be selected as the currently 'active' one. For now, I've set the term 'Coca-Cola Series - Atlanta' as active from its edit screen.

In the future, you can uncheck this field for this term and select the other one, as needed."

I'm not seeing where this is defined. Where do I go to uncheck Atlanta and check the next one to make it the default current tournament?

Thanks to both you and Minesh (and previously Luo Yang) for all your willingness to help me push Toolset's capabilities in all these weird/unique ways on multiple websites I'm handling. 😉

#2639763

Thank you for your kind words and glad that we're able to help.

Please go to WP Admin -> Matches -> Tournaments and you'll be able to use the 'Edit' link to open your target term's edit screen.
( screenshot: hidden link )

On this term edit screen, you'll find this newly defined term field.
( screenshot: hidden link )

Note: The field group can be found at WP Admin -> Toolset -> Custom Fields -> Term Fields
( screenshot: hidden link )

#2639897

Oh OK! I see it now. Very cool! I didn't even know taxonomy terms could have their own fields. Yeah, that makes it to where I only have to do two uncheck/check edits on the tournament terms to change what comes up. That's perfectly fine.

Question: If we have two tournaments running simultaneously, I could check this Is Current field for both of them and they'll both show up -- is that correct? If so then this is extremely perfect because I can see us wanting to have more than one on here in the future.

So now we have this on the Schedule page. Can we also add this to the Results page? Rather than doing it for me, can you describe what I have to do to make this change? I'm hoping I can understand fully how to do this because we may have future Views/pages where this is needed. I'd prefer to limit the number of support tickets I file because I'm feeling guilty about the level of support we've gotten from you on all of this.

Would I just have to change this line of your custom code

	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 832) ) {

to

	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 832, 841) ) {

Let me know if there is more to it than that. I'm hoping that will be all I have to do.

#2640083

We're here to help and it is great that you're willing to learn to do things on your own 🙂

Part 1 - For making the 'Schedule' view's 'Filter by Tournament/League' support multiple terms:

The first change would be that the field 'Filter by Tournament/League' will need to be changed from a 'select' field to either a 'checkboxes' type or a 'multi-select' type field. Because the select type field can only select a single term.

Next, you'll adjust the output of the source view 'view-to-get-the-current-tournament-term-slug', so that it can return the multiple active term slugs in a comma-separated list format.

In the following line of the code snippet, where '$target_term_slug' is getting the output, you can then 'explode' that list into an array:


$target_term_slug = do_shortcode('[wpv-view name="view-to-get-the-current-tournament-term-slug"]');

And in the line where the query is setting the target term slugs, you'll pass the exploded array, in place of '$target_term_slug':


$query_args['tax_query'][0]['terms'][0] = $target_term_slug;

You'll also adjust the following line, to make sure it is setting all the target terms slugs in URL encoded way:
( This part ensures that when default active filtering is applied, relevant options are also shown as selected in the search form )


$_GET['wpv-tournament']=$target_term_slug;

Part 2: Extending the code snippet to multiple views:

As it is, the code snippet will only apply whenever/wherever the view 'Schedule' ( ID: 832 ) is used.

To extend it to target multiple views, you'll replace:


if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 832) ) {

With:


$target_views = array( 832, 841 );

if ( !is_admin() && ( isset($view_settings['view_id']) && (in_array($view_settings['view_id'], $target_views))) ) {

Note: I've added a new array '$target_views' to declare my target view IDs and updated the condition to see if one of the target views is the current view, through 'in_array'.

#2640409

Once again, I am following everything you said here, but everything you've shown is well beyond my skill level to figure out myself. I was hoping that some of this would result in me being able to emulate this to do other things that might be needed, but I didn't even have the code right for making it apply in multiple views. As is, I think the best I'll be able to do from this is copy and paste what you did and add additional view iDs as needed.

I do understand making the filter multi-select. as well. That's easy enough.

So let's see if I'm able to change the Schedule setup to do this AND set it up on the Results page.

Hmmmm, I just changed the filter from select to multi-select and then to checkboxes. I'm not liking how the options all display and take up so much vertical space there. If we get to where we have 30 tournaments, that filter area will be enormous vertically. I know I could probably style it to display inline so the options would spread out to the content area width, but I think that will get too cluttered as well. I think we'll be better off just keeping it as single select on the most important current tournament and let people use the filter to get to any others.

If the multi-select display was a regular dropdown that only shows one line and then you can pick multiple options from within the dropdown by using the CTRL key, I think I'd have gone with that. But I think keeping the single line dropdown is more important here than being able to show multiple tournaments by default.

So now all I have to do is edit the custom code you provided to allow for it to apply to the Results view as well as the Schedule view. That's easy enough and I've successfully done that.

Just to be clear on this, if I want to add this functionality to any other view, am I editing the code as follows...

$target_views = array( 832, 841,###,###,### );
 
if ( !is_admin() && ( isset($view_settings['view_id']) && (in_array($view_settings['view_id'], $target_views))) ) {

where the ### represents any new view IDs I want to use?

#2640577

Thank you for sharing these observations and they make sense.

Your understanding about including more View IDs in the code snippet is also correct.

Feel free to mark this ticket as resolved and you're welcome to start a new one for each new question or concern.

#2642413

Thanks a bunch!