[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.
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.
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.
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.
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.
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.
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.
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.
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:
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.