Skip Navigation

[Resolved] Split: Filter to exclude posts with certain taxonomy terms from view results

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.

Our next available supporter will start replying to tickets in about 1.08 hours from now. Thank you for your understanding.

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

Last updated by SteBlood 1 year, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2469233

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

#2469241

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 suggest the best approach to achieve this, I'll need to see how this view and the taxonomy in question are set up.

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

I'll be in a better position to suggest the next steps, accordingly.

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

regards,
Waqar

#2470349

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing these details.

To additionally exclude the holiday park posts, where all the related holiday home posts have the "Status" taxonomy term "Sold", you can extend the custom code to:


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'] == 109) ) {
		// change these values, as per your website
		$target_post_type = "holiday-park";
		$relationship_slug = "holiday-park-holiday-home";

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

		// get all related posts of the target posts
		foreach ($posts_array as $post_array ) {
			$get_results = toolset_get_related_posts( $post_array -> ID, $relationship_slug, 'parent', 999999, 0, array(), 'post_id', 'child' );
			// if the related post doesn't exist, remove the target post from the view's results
			if(empty($get_results)) {
				$query_args['post__not_in'][] = $post_array -> ID;
			} else {
				//if the related post exists, check if all of them are sold
				$sold_count = 0;
				foreach ($get_results as $get_result) {
					if (has_term( 'Sold', 'home-status', $get_result)) {
						$sold_count++;
					}
				}
				//if all of them are sold, remove the target post from the view's results
				if( $sold_count == count($get_results) ) {
					$query_args['post__not_in'][] = $post_array -> ID;
				}
			}
		}
	}
	return $query_args;
}

And then you can add similar changes to the code snippet for the view with the map.

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/

#2470351

Waqar
Supporter

Languages: English (English )

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

Thank you for sharing these details.

To additionally exclude the holiday park posts, where all the related holiday home posts have the "Status" taxonomy term "Sold", you can extend the custom code to:


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'] == 109) ) {
		// change these values, as per your website
		$target_post_type = "holiday-park";
		$relationship_slug = "holiday-park-holiday-home";

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

		// get all related posts of the target posts
		foreach ($posts_array as $post_array ) {
			$get_results = toolset_get_related_posts( $post_array -> ID, $relationship_slug, 'parent', 999999, 0, array(), 'post_id', 'child' );
			// if the related post doesn't exist, remove the target post from the view's results
			if(empty($get_results)) {
				$query_args['post__not_in'][] = $post_array -> ID;
			} else {
				//if the related post exists, check if all of them are sold
				$sold_count = 0;
				foreach ($get_results as $get_result) {
					if (has_term( 'Sold', 'home-status', $get_result)) {
						$sold_count++;
					}
				}
				//if all of them are sold, remove the target post from the view's results
				if( $sold_count == count($get_results) ) {
					$query_args['post__not_in'][] = $post_array -> ID;
				}
			}
		}
	}
	return $query_args;
}

And then you can add similar changes to the code snippet for the view with the map.

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/

#2471001

Waqar, this is simply amazing.

It worked perfectly, as you probably expected.

Then I wanted to expand on that and remove 2 of the taxonomies, 'Draft / Save for later' and 'Sold'.

So I tried copying from } else { downwards and that didn't work.
I tried adding an extra taxonomy in brackets like ('Sold', 'Draft / Save for later').

That didn't work either.

I added the full code again and made it unique to 'Draft / Save for later' and it works how I want.
Now all posts that are in 'Draft / Save for later' and 'Sold' do not show.

Even though I have my end result, is this the best way to do this?

What I don't want to do is bugger things up for me in the future.

#2471145

Waqar
Supporter

Languages: English (English )

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

Thanks for the update and glad that it worked.

To check for the multiple taxonomy terms through the same code snippet, you can update the line that uses the "has_term" function, from:
( ref: https://developer.wordpress.org/reference/functions/has_term/ )


if (has_term( 'Sold', 'home-status', $get_result)) {

To:


if (has_term( array('Sold', 'Draft / Save for Later'), 'home-status', $get_result)) {

Additionally, you don't have to use two different code snippets, to apply the same filtering to, two different views (one for normal and one for the map only).

You can use the same snippet to target multiple views, by changing the line:


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

To:


$target_views = array( 5940, 5938 );
if (  (isset($view_settings['view_id'])) && (in_array($view_settings['view_id'], $target_views))  ) {

This way, the same code snippet will apply to both views (with ID "5940" & "5938").

#2471557

Mate, I was soo close myself, thank you very much, I've learned something myself today as well!

Enjoy your weekend you Legend of a human being!

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