Skip Navigation

[Resolved] Filter by taxonomies EXCEPT for these related items

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 0 replies, has 1 voice.

Last updated by katjaL 1 month, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2795266

In the product catalogue (WooCommerce) there are multiple taxonomies by which I filter documents (custom post type) to products (classic views -> product content template).

Now there might be - lets say 100 - products belonging to a category which all have this certain document attached (taxonomy filter), but one product in the same category shouldn't show this document.

I thought I could make it this way: the product and the document are related and within this relation I have this custom field "don't show with this product". But I don't know how to make it. Now I have in Query Filter:
- Select posts with taxonomy:
tax1 the same as the page where this view is shown
OR
tax2 te same as the .... and so on

I would like to add:
and check if theres a relation between current product and document AND the custom field value is 0

Can you help me, is it doable even?

Thank you in advance.

#2795268

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

First of all - I would like to review your structure and on what post/page you would like to exclude what posts and why. I already read your initial post but it would be great if you can setup a text example with couple of taxonomy and terms and send me problem URL and admin access details.

Once I review all those details I will be able to guide you in the right direction.

*** 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.

#2795396

Minesh
Supporter

Languages: English (English )

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

As I understand - for the following product:
=> hidden link

You want to check (in this case) if theres a relation between the above product and document post type which there is and the document "DoP Gunreben - Sauvat 1" is connected to product and what custom field you want to check and for what value?

And do you want to exclude or include when condition is matched?

#2795521
Screenshot 2025-02-06 at 8.46.17.png

Yes, there is a relation, check screenshot and the relation custom field "Poista dokumentti tästä tuotteesta" is activated. When this happens the document shouldn't show in this particular product (exclude I suppose).

#2795537

Minesh
Supporter

Languages: English (English )

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

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

We use the view's filter hook "wpv_filter_query" to modify the view's query on fly and to exclude the required post:

add_filter( 'wpv_filter_query', 'func_many_to_many_exclude_related_post', 101, 3 );
function func_many_to_many_exclude_related_post( $query_args, $view_settings, $view_id ) {
  global $post;

  $post_id = $post->ID;
  $relationship_slug = 'product-dokumentti';
  $exclude_doc_ids = array();

  if ( $view_id == 58997 ) {
    
    $exclude_doc_ids = toolset_get_related_posts(
        $post_id,
        $relationship_slug,
        'parent',
        1000000,
        0,
        array(
        	array('meta_key' => 'wpcf-poista-tasta',
            'meta_compare' => '=',
            'meta_value' => '1')
        ),
        'post_id',
        'child'
        );
    
     if(!empty($exclude_doc_ids)){
       $query_args['post__not_in'] = $exclude_doc_ids;
     }
     
  }
   
  return $query_args;
}

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset

#2795561

Wow Minesh, thank you so much!