Skip Navigation

[Resolved] Taxonomy filter with AND instead of OR

This support ticket is created 6 years, 3 months 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.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 10 replies, has 2 voices.

Last updated by Leonardo 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1097918

in according a your advise, I am usiging a taxonomy for filter many post type, but Ineed some taxonomy with filter "AND" and someone with relation "OR", but I see all taxonomies with "AND"
How can I modify that?
regards

#1097920

Hi, the Query Filter builder in wp-admin provides a simple term selection interface. If you want to create more complex AND / OR conditions, the best way to handle that is with custom code using our API: https://toolset.com/documentation/programmer-reference/views-filters

#1097954

hi,
i didn't understant how can I add filter by taxonomy "AND" and "OR"
regards

#1098011

It is not possible in wp-admin. It is only possible with custom code using the Views filters I mentioned. I might be able to help create some code for you if you tell me which term slugs are "AND" and which term slugs are "OR". Please tell me exactly how the filter should be set up, which terms and taxonomies are used, and how they are grouped by AND / OR.

#1098477

Hi Chrstian,
I have this code:

++++++++++++++++

[wpv-filter-start hide="false"]
[wpv-filter-controls]

<div class="section">

<div class="section-row">

<div class="article">

<div class="form-group">
<label>[wpml-string context="wpv-views"]ZONE[/wpml-string]</label>
[wpv-control-post-taxonomy taxonomy="zone" type="checkboxes" format="%%NAME%% : %%COUNT%%" url_param="wpv-zone"]
</div>

</div>

<div class="article">

[wpv-control-postmeta field="wpcf-offerta-favilleo" type="checkbox" url_param="wpv-wpcf-offerta-favilleo"]

[wpv-control-post-taxonomy taxonomy="costo" type="checkboxes" format="%%NAME%%: %%COUNT%%" url_param="wpv-costo"]

</div>

<div class="article">

[wpv-control-postmeta field="wpcf-accessibile" type="checkbox" url_param="wpv-wpcf-accessibile"]
[wpv-control-postmeta field="wpcf-problematiche-alimentari" type="checkbox" url_param="wpv-wpcf-problematiche-alimentari"]
[wpv-control-postmeta field="wpcf-non-udenti" type="checkbox" url_param="wpv-wpcf-non-udenti"]

</div>

</div>
<div class="section-row">
<div class="article">

LOCALI: [wpv-control-post-taxonomy taxonomy="tipo-locali" type="checkboxes" format="%%NAME%%: %%COUNT%%" url_param="wpv-tipo-locali"]
</div>
<div class="article">
INTRATTENIMENTI: [wpv-control-post-taxonomy taxonomy="tipo-intrattenimento" type="checkboxes" format="%%NAME%%: %%COUNT%%" url_param="wpv-tipo-intrattenimento"]
</div>

<div class="article">
LUOGHI:
[wpv-control-post-taxonomy taxonomy="tipo-luogo" type="checkboxes" format="%%NAME%%: %%COUNT%%" url_param="wpv-tipo-luogo"]
</div>
</div>

</div>

[/wpv-filter-controls]

[wpv-filter-end]

++++++++++++++

I need filter "OR" for: wpv-tipo-luogo, wpv-tipo-intrattenimento, wpv-tipo-locali

I need filter "AND" for : wpv-zone , wpv-costo, wpv-wpcf-accessibile wpv-wpcf-non-udenti, wpv-wpcf-problematiche-alimentari
thanks a lot
L.

#1099597

Okay thanks for the additional information. I don't have a simple code snippet that will help you combine multiple taxonomies and custom fields in complex AND / OR conditions like this in a custom search View. Unfortunately our software is not set up to allow this type of filtering. It will require advanced PHP and JavaScript custom code. If you need professional assistance with this code, I suggest you look for a professional developer who can use our APIs to achieve exactly what you need. We have a portal available where you can connect with independent contractors who may be able to help: https://toolset.com/contractors

#1100323

hello,

can you send me code for to combine only multiple taxonomies in AND / OR conditions?

and can you send me code for to combine only custom fields in AND / OR conditions?
regards

#1100650

I'm using examples from the WordPress documentation for WP_Query, applied using our wpv_filter_query API: https://codex.wordpress.org/Class_Reference/WP_Query

An example to create a complex AND/OR condition with multiple taxonomies/terms:

add_filter( 'wpv_filter_query', 'complex_taxonomy_query',99,3 );
function complex_taxonomy_query( $query_args,$views_settings, $view_id) {

  if ($view_id == 12345){
    $args = array(
      'tax_query' => array(
		'relation' => 'OR',
		array(
			'taxonomy' => 'category',
			'field'    => 'slug',
			'terms'    => array( 'quotes' ),
		),
		array(
                        'relation' => 'AND',
                        array(
			        'taxonomy' => 'post_format',
			        'field'    => 'slug',
			        'terms'    => array( 'post-format-quote' ),
                        ),
                        array(
                                'taxonomy' => 'category',
                                'field'    => 'slug',
                                'terms'    => array( 'wisdom' ),
                        ),
		),
	),
    );
  }
  return array_merge( $query_args, $args );
}

An example to create a complex AND/OR condition with multiple custom fields:

add_filter( 'wpv_filter_query', 'complex_field_query',99,3 );
function complex_field_query( $query_args,$views_settings, $view_id) {

  if ($view_id == 12345){
    $args = array(
      'meta_query' => array(
		'relation' => 'OR',
		array(
			'key'     => 'wpcf-color',
			'value'   => 'orange',
			'compare' => '=',
		),
                array(
                        'relation' => 'AND',
                        array(
                                'key' => 'wpcf-color',
                                'value' => 'red',
                                'compare' => '=',
                        ),
                        array(
                                'key' => 'wpcf-size',
                                'value' => 'small',
                                'compare' => '=',
                        ),
		),
	),
    );
  }
  return array_merge( $query_args, $args );
}

Types custom fields use a prefix "wpcf-" in the database, so you must include "wpcf-" here in the filters.

#1100665

hi,
so do I have to insert that code in function.php file ?
sorry but it doen't clear, can you write me a example with my taxnomies :
my taxnomies are: wpv-tipo-luogo, wpv-tipo-intrattenimento, wpv-tipo-locali and I need "OR" condition only for 'wpv-tipo-intrattenimento ' and "AND" condition for wpv-tipo-luogo , wpv-tipo-locali
regards

#1100770

so do I have to insert that code in function.php file ?
Yes, custom code goes in functions.php in your child theme.

sorry but it doen't clear, can you write me a example with my taxnomies :
I'm sorry I don't have a cut-and-paste solution for you. The documentation for WordPress's WP_Query is available here:
https://codex.wordpress.org/Class_Reference/WP_Query
If you need code written specifically for your site using your taxonomies and terms, you need a professional developer. The customization you want is beyond the scope of support we provide here in the forums.
https://toolset.com/toolset-support-policy/

#1100810

Regards