Skip Navigation

[Resolved] View query filter: Exclude posts with specific PARENT ID in a post relationship

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user has many-to-many relationship. He has a view to search inside the child post type with a relationship filters. He wants to exclude the children of a specific parent.

Solution:
Toolset views search for relationships first, then it passes the results in the post__in argument of the view's underlying WP_Query. We can search for the children of the specific parent, then exclude them from the results of the relationship.

Check the example custom code here https://toolset.com/forums/topic/view-query-filter-exclude-posts-with-specific-parent-id-in-a-post-relationship/#post-2217597

Relevant Documentation:

This support ticket is created 3 years 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: Africa/Casablanca (GMT+01:00)

This topic contains 11 replies, has 3 voices.

Last updated by marcusB-3 3 years ago.

Assisted by: Jamal.

Author
Posts
#2207545
Screen Shot 2021-10-27 at 5.44.40 PM.png

I understand how to exclude a post ID using a query filter, but in my case, it's simply excluding an intermediary post. I need to be able to exclude a post with a specific relationship parent ID.

Is this something I would need to do with a custom function?

I simply need to define which views to apply the query to. Provide a specific POST ID of the relationship parent, then when I run the view, it will filter out any posts that have that particular ID as the parent.

thank you!

marc

#2207935

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I will first require to review your current relationship and setup and would like to know what post you would like to exclude. If you can share all details and problem URL and access details I would be happy to review and later will share possible solution if exists.

Please share what you what to exclude and what you want to include.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2208429
Screen Shot 2021-10-28 at 3.57.16 PM.png

Hi, because of the private nature of this development (personal health data), I hope you can consider giving this a try without access. I'll keep it very simple!

1. I have a many to many relationship called "supplement-condition".
2. This relationship creates an intermediate post.
3. I have a view that lists all of the supplements related to a condition:
hidden link

4. This view queries the post type: Supplements Conditions Intermediary Posts
5. I am using a query filter to filter by the "supplement conditions" post relationship.
6. I have a particular supplement called "None" that I wish to exclude when filtering for results.
7. This is not currently possible because "None" is part of an intermediate post type and I am unable to EXCLUDE anything when only querying intermediate post types.
8. I'm guessing I need a coded query that runs for this particular view that looks for any relationship that includes "None" as the parent of the relationship and excludes it.

I hope that makes sense!

marc

#2208723

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As I understand you have a post namely "None" and that post is belongs to intermediary post type - correct? If yes:
- Then what you shared in the initial post what if you add a post ID filter and exclude that specific "None" post ID as the view is set to query the intermediary post type you can use the post ID filter to exclude the "None" post ID.

Is that not working what you mean?

#2208761

Hi,

That doesn't work because I am querying intermediate post types and NONE is not an intermediate post, it's the parent in a relationship:

supplements (parent) --> conditions (child)

So when I run the view it is querying all intermediary post types related to a specific condition.

If one of those intermidate posts has "NONE" as the parent (supplement), we want to exclude it.

marc

#2208775

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to know are you just displaying the content without pagination - correct?

There are I think multiple ways to achieve this. One way is you can use the [wpv-conditional] shortcode to check the parent ID is not equal to None post and then you can display the post.

Will that work for you? If yes:
- You should try to wrap the code with the [wpv-conditional] shrotcode. Considering the "None" post ID is 9999.

[wpv-conditional if="( '[wpv-post-id item="@supplement-condition.parent"]' ne '9999' )"]
 Display the content
[/wpv-conditional]

You should replace the 9999 with the original "None" post ID.

#2208781

Hi, yes I tried that and it works, but it leaves a blank instead of filtering it out...

as you can see I have a ranking number 1-20

hidden link

If I use the conditional, it would skip a number.

Any way to do this with a function?

marc

#2208799

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To filter the results we can use the view's hook "wpv_filter_query_post_process".

Can you please try to add the following code to "Custom Code" section offered by Toolset and make sure you activate the snippet.
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

add_filter( 'wpv_filter_query_post_process', 'func_filter_parent_none_post', 10, 3 );
function func_filter_parent_none_post( $query, $view_settings, $view_id ) {
    if ( !empty( $query->posts ) ) { 
         
      $all_posts = $query->posts;
      foreach($query->posts  as $k=>$v):
		$parent_ids = toolset_get_related_posts(
						$v->ID,
						'supplement-condition',
						'intermediary',
						1000000,0,
						array(),
						'post_id',
						'parent'
					  );
			 if(in_array(999999,$parent_ids)){
				 unset($all_posts[$k]);
			}

      endforeach;

		$query->posts = $all_posts; 
		$query->found_posts = count($all_posts); // modify the count of found posts
              $query->post_count = count($all_posts); // modify the count of displayed posts
    }
    return $query;
}

Where:
- Replace 999999 with your original "None" parent ID.

More info:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

#2209041

Thank you for the code! Unfortunately it is giving me this critical error. Any idea why?

thanks,
marc

Notice: Undefined offset: 2 in /www/whathelps_132/public/wh-files/modules/wp-views/embedded/inc/wpv.class.php on line 2565

Notice: Undefined offset: 2 in /www/whathelps_132/public/wh-files/modules/wp-views/embedded/inc/wpv.class.php on line 2569

Fatal error: Uncaught Error: __clone method called on non-object in /www/whathelps_132/public/wh-files/modules/wp-views/embedded/inc/wpv.class.php:2569 Stack trace: #0 /www/whathelps_132/public/wh-files/modules/wp-views/embedded/inc/wpv.class.php(2124): WP_Views->render_view('23443', 'f956ad9ee676c4b...') #1 /www/whathelps_132/public/wh-files/modules/wp-views/embedded/inc/wpv.class.php(639): WP_Views->render_view_ex('23443', 'f956ad9ee676c4b...') #2 /www/whathelps_132/public/wp-includes/shortcodes.php(356): WP_Views->short_tag_wpv_view(Array, '', 'wpv-view') #3 [internal function]: do_shortcode_tag(Array) #4 /www/whathelps_132/public/wp-includes/shortcodes.php(228): preg_replace_callback('/\\[(\\[?)(wpv\\-v...', 'do_shortcode_ta...', '[wpv-view name=...') #5 /www/whathelps_132/public/wh-files/themes/blankslate/header.php(50): do_shortcode('[wpv-view name=...') #6 /www/whathelps_132/public/wp-includes/template.php(770): require_once('/www/whathelps_...') #7 /www/whathelps_132/public/wp-includes/template.php(716): load_temp in /www/whathelps_132/public/wh-files/modules/wp-views/embedded/inc/wpv.class.php on line 2569
There has been a critical error on this website.

#2210015

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'll require admin access with problem URL details and you should also tell me where exactly you added the code I shared.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2217597

Hello marc! Minesh won't be available for a couple of days. If you don't mind, I'll continue with you on this ticket.

I think that the best solution is to filter the query arguments and exclude the related posts to "None" instead of removing it after the query is executed. I run a small test on the provided website and I come up with the following code:

add_filter( 'wpv_filter_query', 'exclude_posts_related_to_none', 30, 3 );
function exclude_posts_related_to_none( $query_args, $view_settings, $view_id ) {
  $none_post_id = 24161;
  if ( $view_id == 105 ) {
    $none_related_posts = toolset_get_related_posts( $none_post_id, 'supplement-condition', array(
      'query_by_role' => 'parent',
      'role_to_return' => 'intermediary',
    ));
    
    $query_args['post__in'] = is_array( $query_args['post__in'] ) ? $query_args['post__in'] : array();
    $none_related_posts = is_array( $none_related_posts ) ? $none_related_posts : array();
    
    $query_args['post__in'] = array_diff( $query_args['post__in'], $none_related_posts );
  }
  
  return $query_args;
}

Toolset stores relationships in its own database tables. During a view process, Toolset runs relationship queries first, then pass the results to the post__in argument of a regular WP_Query instance.

We can filter the arguments of the query and remove the posts that we know are related to the post "None". As you can see in the code, we can get the related posts to "None" using the toolset_get_related_posts function. Read more about it here https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Then we can remove thoses IDs from "post__in" argument using the array_diff function. Read more about the wpv_filter_query hook here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

I hope this helps. Let me know if you have any questions.

#2218001

My issue is resolved now. Thank you!