Skip Navigation

[Resolved] Create View To Display Post With Relationship

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

This topic contains 2 replies, has 2 voices.

Last updated by Waqar 2 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2318621

I created a view and it is now showing all posts from [A], with and without a relationship with [B].

I am trying to create a view to show all post from [A] that has a relationship with [B].

Post type [A] and [B] has a one-to-many relationship.

#2319065

Hi,

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

Do you need to show the posts from post type [A] which are related to:
- any specific post from a post type [B]
or
- any post from post type [B]?

Also if you could share temporary admin login details, along with the link to the page with this view, I'll be in a better position to see how the view is set up and suggest the next steps, accordingly.

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

regards,
Waqar

#2320847

Thank you for sharing these details.

To filter out the "Agent" posts, without any "Review" posts, you'll need a custom function attached to the filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

The custom function will cycle through all the agent posts and look for their related review posts, and for the ones without any related posts, set them for the exclusion by the view's query.

For example:


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

	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 35570) ) {

		$post_type = "agent";
		$relationship_slug = "agent-review";

		$args = array(
			'posts_per_page'   => -1,
			'post_type'        => $post_type,
			'post_status'      => 'publish',
		);

		$posts_array = get_posts( $args );

		foreach ($posts_array as $post_array ) {

			$get_results = toolset_get_related_posts( $post_array -> ID, $relationship_slug, 'parent', 1, 0, array(), 'post_id', 'child' );

			if(empty($get_results)) {
				$query_args['post__not_in'][] = $post_array -> ID;
			}
		}
	
	}

	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.

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/

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