Skip Navigation

[Résolu] Having trouble filtering and returning related results based on dropdown menu

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to filter view and return the related posts with new Many to Many post relationship API

Solution:
You can use view's filter wpv_filter_query to filter view and return the related posts.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/having-trouble-filtering-and-returning-related-results-based-on-dropdown-menu/#post-901080

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#full-documentation-of-the-query-argument

This support ticket is created Il y a 5 années et 10 mois. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 5 réponses, has 2 voix.

Last updated by webD-3 Il y a 5 années et 10 mois.

Assisted by: Minesh.

Auteur
Publications
#894329
Capture.JPG

I'm building a team roster and I've created a many-to-many relationship, so "roster years" are related to "players". A roster year example is "2018/2019". It can be assigned to many players and players can be assigned to up to 4 roster years.

I have a dropdown setup in the view "Search and Pagination" area to display the roster years. It's working fine.

[wpv-filter-start hide="false"]
[wpv-filter-controls]
[wpml-string context="wpv-views"]Season:[/wpml-string][wpv-control url_param="wpv-roster-season" type="select" values="[etc-add-seasons]" display_values="[etc-add-seasons]"]
[/wpv-filter-controls]
[wpv-filter-end]

The function related to the dropdown is:

////////////////////////
// GET ROSTER SEASONS //
////////////////////////
add_shortcode('etc-add-seasons', 'etc_get_roster_seasons');
function etc_get_roster_seasons(){
	$query = new WP_Query(
		array(
		'post_type' => 'roster-year',
		'posts_per_page' => -1,
		'orderby' => 'title',
		'order' => 'DESC',
		)
	);
	$arr = array();
	while ($query->have_posts()){
		$query->next_post();
		$arr[] = get_the_title($query->post->ID);
	}
	
	foreach($arr as $season){
		$seasons = $seasons . $season . ',';
	}
	return rtrim($seasons, ",");
}

Where I need help is writing the filter function to display the players that are related to the roster season value that's selected in the dropdown menu.

///////////////////////////////////////////////
// FILTER THE ROSTER VIEW BY SELECTED SEASON //
///////////////////////////////////////////////
function roster_season_func( $query_args, $settings, $view_id ) {
	$ddl = $_GET['wpv-roster-season'][0];
    if ( $view_id == 259 && isset($ddl) && !empty($ddl) ) {
		//do something here to return players that are related to the selected roster year from the dropdown menu mentioned up above
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'roster_season_func', 20, 3 );
#895507

Minesh
Supporter

Languages: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

Well - In order to help you with this issue I need to check what values configured with select dropdown.

Could you please share problem URL where you added the view and one test case example that if we select X option from dropdown what should be your expected output.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#900950

Minesh
Supporter

Languages: Anglais (English )

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

It seems you are changing the functions.php file. Please let me know once you finish your changes as it will result in conflicts.

#900952

Yes, I was testing some things to see if I could figure out a way to get the roster filter function to work. I won't make any more adjustments until I hear back from you now.

#901080

Minesh
Supporter

Languages: Anglais (English )

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

Ok - I've adjusted the code in your functions.php file as given under:

///////////////////////////////////////////////
// FILTER THE ROSTER VIEW BY SELECTED SEASON //
///////////////////////////////////////////////
function roster_season_func( $query_args, $settings, $view_id ) {
	$ddl = $_GET['wpv-roster-season'][0];
    if ( $view_id == 259 && isset($_GET['wpv-roster-season'][0]) && !empty($_GET['wpv-roster-season'][0]) ) {
		//do something here to return players that are related to the selected roster year
		/*$query_args['post_type'] = 'player';
		$query_args['toolset_relationships'] = array(
			'role' => 'child',
			'related_to' => get_the_ID(),
			'relationship' => array('roster-year','player'),
		);*/
		$post = get_page_by_title($ddl, OBJECT, 'roster-year');
		
		
		  $query = new WP_Query( 
								 array(
								  'post_type' => 'player',
								  'fields'=>'ids',
								  'numberposts' => -1,
								  //new toolset_relationships query argument
								  'toolset_relationships' => array(
								   'role' => 'child',
								   'related_to' => $post->ID,
								   'relationship' => 'roster-year-player'
								  ),
								  ));
		$posts = $query->posts;
		
		
		$ids = 0;
		if(!empty($posts)){
			
			$query_args['post__in'] = $posts;
		}
			
  }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'roster_season_func', 10, 3 );

I can see now that when you filter the view with year, its displaying the correct results. Could you please confirm.

#901090

Thank you, Minesh! That's working great now!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.