Passer la navigation

[Résolu] Hoc can I add OR condition in query filter search instead AND operator?

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

Problem:

The issue here is that the user wanted their custom query filter to function like an OR query instead of the default AND..

Solution:

Previously you were allowed to change the field comparison logic from AND to OR and vice versa but it got broken somehow in a recent update.

To work around this you're going to have to construct the query manually using the views hook. Add the following to your Toolset Custom Code section and it should help to resolve the issue.

The Toolset custom codes can be found at Toolset -> Settings -> Custom Code

add_filter( 'wpv_filter_query', 'filter_by_multiple_fields', 10, 3 );
  
function filter_by_multiple_fields( $query, $settings, $view_id ) {
    if ( $view_id == 1234) {
       $query['meta_query'] = array(
                'relation' => 'OR',
                array(
                    'key'     => 'color',
                    'value'   => '1',
                    'compare' => '=',
                ),
                array(
                    'key'     => 'price',
                    'value'   => '1',
                    'compare' => '=',
                ),
        );
    }
    return $query;
}

Now replace the 1234 with the ID of your view and then replace color and price with the slugs of the field that you are doing the query on. Remember to add the wpcf- prefix to the types custom fields slugs.

This support ticket is created Il y a 4 years, 12 months. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Fuseau horaire du supporter : America/Jamaica (GMT-05:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par annaA-2 Il y a 4 years, 12 months.

Assisté par: Shane.

Auteur
Publications
#2083115
Screenshot 2021-06-10 at 13.59.44.png

Hi,
I'm using custom search and I need to filter results according to two kinds of conditions but they are related to different kink of posts type.
How can I set OR condition instead of AND condition in content selection?
Thank you in advance for your help.

#2083765

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Anna,

Thank you for getting in touch.

Previously you were allowed to change the field comparison logic from AND to OR and vice versa but it got broken somehow in a recent update.

To work around this you're going to have to construct the query manually using the views hook. Add the following to your Toolset Custom Code section and it should help to resolve the issue.

The Toolset custom codes can be found at Toolset -> Settings -> Custom Code

add_filter( 'wpv_filter_query', 'filter_by_multiple_fields', 10, 3 );
 
function filter_by_multiple_fields( $query, $settings, $view_id ) {
    if ( $view_id == 1234) {
       $query['meta_query'] = array(
				'relation' => 'OR',
				array(
					'key'     => 'color',
					'value'   => '1',
					'compare' => '=',
				),
				array(
					'key'     => 'price',
					'value'   => '1',
					'compare' => '=',
				),
		);
    }
    return $query;
}

Now replace the 1234 with the ID of your view and then replace color and price with the slugs of the field that you are doing the query on. Remember to add the wpcf- prefix to the types custom fields slugs.

Thanks,
Shane

#2084283

My issue is resolved now. Thank you!