Skip Navigation

[Resolved] Split: How to show the related posts through view

This support ticket is created 3 years, 8 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
- 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)

Author
Posts
#1983227

Hi Waqar,

We can now connect multipe "vakexperts" to an "introducties" post so that works, HOWEVER,

What query filter should we apply to > "view" > "content selection" > "introducties" to ONLY SHOW the "introducties" posts to "vakexperts" that have been connected to them?

This is the page we created: hidden link.

We tried a lot of filters but none work so far.

#1983239

Hi,

I've checked the page "Introducties ingelogde gebruiker" and see that it includes a view to show the "Introducties" posts.

However, since this is a regular page and not a single post page for "Introducties" post type or the "vakexperts" post type, I'm not sure which post you'd like to use as a reference, to show the related posts from?

Can you please share some more details about the exact requirement you have in mind from this view?

regards,
Waqar

#1983439

Waqar,

I am quite unsure if I understand your question.

I'll break down my issue once again below:

The page "Introducties ingelogde gebruiker" includes a view to show the "introducties" posts BUT the page should ONLY SHOW vakexperts the "introducties" posts for which they have been "connected".

Here's an example:

Let's say there are the following introducties (post type "introducties"):

- introductie A.
- introductie B.
- introductie C.

Vakexpert A (post type "vakexperts") has been connected to introductie A and B.

Vakexpert A should ONLY SEE introductie A and B on the page "Introducties ingelogde gebruiker" AND NOT introductie C.

So my question remains: what query filter or specific settings should we apply to achieve the above behavior?

I hope this makes sense to resolve this issue.

#1986649

Thank you for sharing these details and I now understand what you mean.

Am I correct to assume that each "vakexperts" user will have a single post in the post type "vakexperts" and he/she will be set as the author of that post?

If not how will the currently logged-in user linked to the "vakexperts" post?

#1986755

Yes, that is right.

#1988581

Thank you for confirming and I'll need to perform some tests on my website, with a similar setup.

I'll share my findings with you, as soon as this testing completes.

Thank you for your patience.

#1989169

Thank you for waiting.

During testing on my website, I was able to filter the view for the "introductie" posts, based on the ID of the current user's "vakexpert" post, using the "wpv_filter_query".
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )

Example:


add_filter( 'wpv_filter_query', 'wpv_filter_query_5663_func', 1000 , 3 );
function wpv_filter_query_5663_func( $query_args, $view_settings ) {
	// skip if blocks edit screen
	if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
		return $query_args;
	}
	
	// process if specific view
	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 5663) ) {
		
		$current_user_id = get_current_user_id();

		if($current_user_id > 0) {
			// get current user's "vakexpert" post
			$args = array(
				'post_type'        => 'vakexpert',
				'posts_per_page'   => 1,
				'post_status'      => 'publish',
				'author'           => $current_user_id,
				'fields'           => 'ids',
			);

			$posts_array = get_posts( $args );

			if(!empty($posts_array)) {
				$query_args['toolset_relationships'] = array( 'role' => 'child', 'related_to' => $posts_array[0], 'relationship' => 'vakexpert-introductie' );
			}
			else
			{	
				// setting the post type to a non-existent post type so that no results are shown
				$query_args['post_type'] = 'xyz';
			}
		}
		else
		{
			// setting the post type to a non-existent post type so that no results are shown
			$query_args['post_type'] = 'xyz';
		}
	}
	return $query_args;
}

The above code snippet 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 the active theme's "functions.php" file.

As a result, the logged-in user will see only the "introductie" posts from the view "Introductie ingelogde gebruikers", which are related to a "vakexpert" where he/she is the author.

And if the current user has no related "introductie" posts or if the visitor is not logged-in, no results will be shown.

#1989197

My issue is resolved now. Thank you Waqar!