I have two posts types:
- Product
- Product Group
Product is a child of Product Group
I want to display these posts but exclude Posts that are a child of a Product Group.
So, if my posts are:
product 1 > product group B
product 2
product 3 > product group A
product 4 > product group A
product 5
I want to see:
Product group B
product 2
product group A
product 5
Please can you tell me how do I include in my Query, exclude products that are a child of a product group? Sequence is not important as I can specify that.
Thanks
Tony
Dear Tony,
I suggest you create a nested view:
1) Parent view list parent "Product Group" posts
add a filter to exclude the specific post ID you want
https://toolset.com/documentation/user-guides/filtering-views-query-by-post-id/
2) Child view list child "Product" posts
filter with child posts setup by parent view:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/
Sorry Luo - I may not have explaing well enough or I am not understanding what needs to be done.
I will simplify:
So, if my posts are:
product 1 > product group B
product 2
product 3 > product group A
product 4 > product group A
product 5
I want to see:
product 2
product 5
These are the only two products that do not have a product group parent.
Regards
Tony
I assume the parent post type "product group" is using slug "product-group", so in each child "product" post there is a hidden field "_wpcf_belongs-product-group_id" to store the parent "product group" post ID.
In your case, you can try with Views filter hook wpv_filter_query, ad a filter by field "_wpcf_belongs-product-group_id" NOT EXISTS
See document:
wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' and 'NOT EXISTS'. Default value is '='.
Thanks Luo
This sounds most helpful and so I will give it a try. May take a couple of days.
I will leave the thread open for now and will update it when I have had chance to try out the solution.
Tony
OK, Please update this thread if you need more assistance for it.
I am afraid I have failed to sort out the code that I need as my programming abilities are very very limited! I tend to look for examples and try to adapt them but cannot find one near enough to what I want to do.
I think I need to put something along the lines of this in functions.php
======================================
add_filter( 'wpv_filter_query', 'remove_products_in_a_product_group' );
function remove_products_in_a_product_group( $query_args ) {
if($setting['view_id'] = 213) {
?
?
?
}
return $query_args;
}
and the above needs to include
"_wpcf_belongs-product-group_id" NOT EXISTS
but I don't know that the ??? should be.
Thanks
Tony
PS
I good set of generic filter examples on the Toolset website would be most useful.
Please try to modify your PHP codes as below:
add_filter( 'wpv_filter_query', 'remove_products_in_a_product_group', 10, 3 );
function remove_products_in_a_product_group( $query_args, $view_settings, $view_id ) {
if($view_id = 213) {
$query_args['meta_query'][] = array(
'key' => '_wpcf_belongs-product-group_id',
'compare' => 'NOT EXISTS',
);
}
return $query_args;
}
Thanks Luo
Nothing happened so I changed _wpcf_belongs-product-group_id to _wpcf_belongs_product-group_id
It then worked but also affected another view so I think either I have a problem (possible) to resolve with my views or the if($view_id = 213) is not working. The other view affected has a different id.
Tony
You are right, please modify the PHP codes as below and test again:
add_filter( 'wpv_filter_query', 'remove_products_in_a_product_group', 10, 3 );
function remove_products_in_a_product_group( $query_args, $view_settings, $view_id ) {
if($view_id == 213) {
$query_args['meta_query'][] = array(
'key' => '_wpcf_belongs_product-group_id',
'compare' => 'NOT EXISTS',
);
}
return $query_args;
}
Thanks Luo - excellent reult and works a treat.
Your help is very much appreciated.
Tony