Skip Navigation

[Résolu] View Query – exclude posts with a parent

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Display posts in child post type which do not have parent post settings.
Solution:
With Views filter hook wpv_filter_query, for example, the parent post type is using slug "product-group", you can add a filter by field "_wpcf_belongs-product-group_id" NOT EXISTS
Here is the example codes:

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;
}

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

This support ticket is created Il y a 7 années et 11 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. 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/Hong_Kong (GMT+08:00)

Marqué : 

This topic contains 10 réponses, has 2 voix.

Last updated by tony Il y a 7 années et 10 mois.

Assisted by: Luo Yang.

Auteur
Publications
#394535

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

#394902

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/

#394968

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

#395204

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 '='.

#395339

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

#395574

OK, Please update this thread if you need more assistance for it.

#395652

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.

#396188

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;
}
#396383

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

#396664

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;
}
#396811

Thanks Luo - excellent reult and works a treat.

Your help is very much appreciated.

Tony

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