Skip Navigation

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

This thread is resolved. Here is a description of the problem and solution.

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 3 years, 5 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.

Our next available supporter will start replying to tickets in about 5.27 hours from now. Thank you for your understanding.

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 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by annaA-2 3 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#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

Languages: English (English )

Timezone: 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!