Skip Navigation

[Resolved] Filter to show only posts that have related posts assigned.

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 8 replies, has 2 voices.

Last updated by Waqar 1 year, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2465959

Hello,

I have two CPT;

Parks
Properties

This is a one to many relationship where there are multiple properties to a park.

I have my archive page which shows all parks, but now I would like to create another search page (view) that shows only parks that have properies assigned to them.

Could you help me with a solution to this please?

Many Thanks

Steve

#2466413

Waqar
Supporter

Languages: English (English )

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

Hi Steve,

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

To exclude the 'Properties' posts from a view, which are not related to any parent "Parks" post, you can use the "wpv_filter_query" filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example, suppose your view's ID is "12345", the slug of the property post type is "property" and the slug of the relationship is "park-property".

The code for this exclusion will look like this:


add_filter( 'wpv_filter_query', 'filter_no_parent_park_posts_fn', 1000 , 3 );
function filter_no_parent_park_posts_fn( $query_args, $view_settings ) {
	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 48) ) {
		// change these values, as per your website
		$target_post_type = "property";
		$relationship_slug = "park-property";

		// cycle through all target posts
		$args = array(
			'posts_per_page'   => -1,
			'post_type'        => $target_post_type,
			'post_status'      => 'publish',
		);
		$posts_array = get_posts( $args );

		// check if the parent post exists and if not remove the target post from the view's results
		foreach ($posts_array as $post_array ) {
			$get_results = toolset_get_related_posts( $post_array -> ID, $relationship_slug, 'child', 999999, 0, array(), 'post_id', 'parent' );
			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.

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

regards,
Waqar

#2467849

Hi Waqar, thank you kindly for this, do I need to change the View ID to the ID of the view I will be using?

Steve

#2468287

Waqar
Supporter

Languages: English (English )

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

Thanks for writing back.

Yes, you'll replace the following items in the code snippet to match your website:

- '12345': with the actual ID of your view
- 'property': with the actual slug of your property post type
- 'park-property': with the actual slug of your relationship between the park and the property post type

#2468345

Hi Waqar,

So after testing thoroughly and nearly writing a long ass post here about how it isn't working the way I wanted, I sat and read your code. Which is great btw!

So what I noticed was it was working correctly, however the opposite way to what I wanted, so I changed the 'parent' and 'child' around then changed the slug from properties to park and viola! it works!

Thanks Waqar! I can now continue with my work 🙂

#2468347

Thanks Waqar!

#2468365
parkshowingeventhoughsold.jpg

So what I have come to realise is that I cannot just show the parks that have 'any' properties linked.

I have a taxonomy which allows people to mark the properties as;

For Sale, Sold, Draft/Save for later or Under Offer.

Anything Sold has been removed from any listings on the website.

However, using this formula works, but it still shows the parks that have attached Sold items.

Is there a way I can also negate 'Sold' items from the search even though they're attached via a relationship?

For example;

You can see each park has 1 Private Sale(s), but Rownhams Park does not.
This is because the one attached to Rownhams Park has been marked as 'Sold', so shouldn't technically show on this list.

Any help with this would be greatly appreciated, I am soo very nearly there with it!

Thanks
Steve

#2468375

Changing the post status from Published to either pending review or draft does not change the outcome of this.

#2469239

Waqar
Supporter

Languages: English (English )

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

Thanks for the update and glad that the code worked.

I've created a separate ticket for your question about excluding the posts, based on the taxonomy term.
( ref: https://toolset.com/forums/topic/split-filter-to-exclude-posts-with-certain-taxonomy-terms-from-view-results/ )

I'll be following up on that ticket shortly and you're welcome to start a new ticket for each new question or concern.

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