Skip Navigation

[Résolu] Views -Select posts in a relationship that are related to any of multiple values

This support ticket is created Il y a 2 années et 5 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.

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

Last updated by tony Il y a 2 années et 5 mois.

Assigned support staff: Waqar.

Auteur
Publications
#1603853

Tell us what you are trying to do?

I have people related to membership statuses.

I want to list all people that are related to any of selected membership statuses but can only select one value.

Example

Fred > Adult
Jim > Adult
Connie > Adult Senior
Annie > Under 18
Billy > Lapsed Member

I want to select all people where membership has not expired (no problem I can do this)
AND
who are an "Adult" or "Adult Senior" or "Under 18"

The selection box only allows me to select a single membership status value.

Please can you assist?

Thanks
Tony

Select items in the membership statuses people relationship
as related items of...

Specific: [Membership Statuses v] Adult v

I am using Views 2.8.1.1 - very wary about updating versions since recent changes

#1605109

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

Your observation is correct and when filtering posts by relationship, only a single item can be selected.

To suggest some workaround, I'll need to see exactly how this relationship and view are set up in the admin area.

Can you please share temporary admin login details along with the link to a page with this view?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1607243

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Tony,

Thank you for sharing these details.

To filter results at the query level, based on the relationship with multiple terms, you can use "wpv_filter_query" filter ( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query ) and "toolset_get_related_posts" function ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts ).

For example, suppose that your View ID is 12345 and the target status posts IDs are 4, 6 & 8 and the involved relationship slug is "membership-status-person", then the function will look like this:


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

	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
		// IDs of the required statuses
		$req_statuses = array(4, 6, 8);

		foreach ( $req_statuses as $post_array ) {
			// get parent post in a relationship
			$query_by_element = $post_array; // ID of post to get relationship from
			$relationship = 'membership-status-person'; // relationship slug
			$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation 
			$limit = 999999; // defaults
			$offset = 0; // defaults
			$args = array(); //nothing needed
			$return = 'post_id'; // We want Post ID
			$role_name_to_return = 'child'; // We want child.

			$get_results = toolset_get_related_posts(
							$query_by_element,
							$relationship,
							$query_by_role_name,
							$limit,
							$offset,
							$args,
							$return,
							$role_name_to_return
							);
			
			foreach ($get_results as $get_result ) {
				$query_args['post__in'][] = $get_result;
			}
		}
	}

	return $query_args;
}

Feel free to adjust the snippet as per your website and it can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1607441

My issue is resolved now. Thank you!