Skip Navigation

[Resolved] How to modify view’s query on fly

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

Problem:
How to modify view's query on fly with "AND" and "OR" clause for custom fields

Solution:

You can find the proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/not-completed-query/page/2/#post-1168622

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 5 years, 4 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.

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 26 replies, has 2 voices.

Last updated by Kostas 5 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1167465

I am trying to: build a cpt custom search.

Link to a page where the issue can be seen: hidden link
If radio Blend is checked at Yes (Ναι, second choice) and you press the search button (ΑΝΑΖΗΤΗΣΗ), then mysql server goes to 100% and the query after a while times out.

I expected to see: something like when the search is made with radio Blend set at default No (Όχι, first choice)

Instead, I got: an empty page.

Please note that when the search is made with Blend set at Yes (Ναι, second choice), the query is constructed using wpv_filter_query filter (in functions.php), as per this ticket:
https://toolset.com/forums/topic/conditional-search-how-to/

#1167522

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - I need to understand here why there are too many query filter argument is added:

array(
                        'relation' => 'OR',
                        array(
                            'key'     => 'wpcf-variety-member1',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member2',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member3',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member4',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member5',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member6',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member7',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
						),
                    )

Could you please check the server logs what is the cause behind the times out error.

I see there are a number of fields added but it needs to check further. What if you try to remove field arguments one by one and check what field argument is causing the issue?

#1167526

Hi Minesh,

Thank you for responding.

The problem started as soon as I added the last filter:

                    array(
                        'key'     => 'wpcf-pungent',
                        'value'   => $_GET['wpv-wpcf-pungent_min'],
                        'type'    => 'NUMERIC',
                        'compare' => '>=',
                    ),
	            array(
                        'key'     => 'wpcf-pungent',
                        'value'   => $_GET['wpv-wpcf-pungent_max'],
                        'type'    => 'NUMERIC',
                        'compare' => '<=',
                    ),

Before that addition it was running fine, although Query Monitor plugin was reporting a slow query (as I found out later).

Kostas

#1167536

Minesh
Supporter

Languages: English (English )

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

What if you try, as I think it needs "AND" clause condition:

array(
   'relation' => 'AND',
array(
        'key'     => 'wpcf-pungent',
        'value'   => $_GET['wpv-wpcf-pungent_min'],
        'type'    => 'NUMERIC',
        'compare' => '>=',
    ),
array(
        'key'     => 'wpcf-pungent',
        'value'   => $_GET['wpv-wpcf-pungent_max'],
        'type'    => 'NUMERIC',
        'compare' => '<=',
    ),
)
#1167547

I tried it, no difference.

Also please note, Apache logs show this (for my dev machine having an exact site copy):

[Mon Dec 17 12:00:15 2018] [error] [client ::1] FastCGI: comm with server "/Applications/MAMP/fcgi-bin/php7.2.8.fcgi" aborted: idle timeout (30 sec), referer: hidden link
[Mon Dec 17 12:00:15 2018] [error] [client ::1] Handler for fastcgi-script returned invalid result code 1, referer: hidden link

#1167580

Minesh
Supporter

Languages: English (English )

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

Well - following query method should work - as it needs "AND" relation with all the greater than and less than query argument which you applied to the same field.:

Could you please try following

$query_args['meta_query'] = array(
                    'relation' => 'AND',
					array('relation' => 'AND',
							array(
								'key'     => 'wpcf-quantity',
								'value'   => $_GET['wpv-wpcf-quantity_min'],
								'type'    => 'NUMERIC',
								'compare' => '>=',
							),
							array(
								'key'     => 'wpcf-quantity',
								'value'   => $_GET['wpv-wpcf-quantity_max'],
								'type'    => 'NUMERIC',
								'compare' => '<=',
							),
					),
                    array(
                        'key'     => 'wpcf-oo-category',
                        'value'   => $_GET['wpv-wpcf-oo-category'],  //
                        'type'    => 'CHAR',
                        'compare' => 'LIKE',
                    ),
					array('relation' => 'AND',
						array(
							'key'     => 'wpcf-oleicacid',
							'value'   => $_GET['wpv-wpcf-oleicacid_min'],
							'type'    => 'NUMERIC',
							'compare' => '>=',
						),
						array(
							'key'     => 'wpcf-oleicacid',
							'value'   => $_GET['wpv-wpcf-oleicacid_max'],
							'type'    => 'NUMERIC',
							'compare' => '<=',
						),
					),
					array('relation' => 'AND',
						array(
							'key'     => 'wpcf-fruity',
							'value'   => $_GET['wpv-wpcf-fruity_min'],
							'type'    => 'NUMERIC',
							'compare' => '>=',
						),
						array(
							'key'     => 'wpcf-fruity',
							'value'   => $_GET['wpv-wpcf-fruity_max'],
							'type'    => 'NUMERIC',
							'compare' => '<=',
						),
					),
					array('relation' => 'AND',
						array(
							'key'     => 'wpcf-bitter',
							'value'   => $_GET['wpv-wpcf-bitter_min'],
							'type'    => 'NUMERIC',
							'compare' => '>=',
						),
						array(
							'key'     => 'wpcf-bitter',
							'value'   => $_GET['wpv-wpcf-bitter_max'],
							'type'    => 'NUMERIC',
							'compare' => '<=',
						),
					),
					array('relation' => 'AND',
						array(
							'key'     => 'wpcf-pungent',
							'value'   => $_GET['wpv-wpcf-pungent_min'],
							'type'    => 'NUMERIC',
							'compare' => '>=',
						),
						array(
							'key'     => 'wpcf-pungent',
							'value'   => $_GET['wpv-wpcf-pungent_max'],
							'type'    => 'NUMERIC',
							'compare' => '<=',
						),
					),
					array(
                        'relation' => 'OR',
                        array(
                            'key'     => 'wpcf-variety-member1',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member2',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member3',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member4',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member5',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member6',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'key'     => 'wpcf-variety-member7',
                            'value'   => $_GET['wpv-wpcf-variety'],
                            'type'    => 'CHAR',
                            'compare' => 'LIKE',
						),
                    ),	
            );

Does above query makes any difference?

#1167594

I 'm afraid not, no difference.

#1167595

Minesh
Supporter

Languages: English (English )

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

Sorry hear that there is no difference. I need access details to see whats causing the issue especially FTP access.

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1167596
#1167598

Minesh
Supporter

Languages: English (English )

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

Well - I know you shared admin access but that will not be helpful as I need to add/remove things from the functions.php file where you added the code. Please share FTP access details so I can work on it.

#1167612
#1167623

Minesh
Supporter

Languages: English (English )

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

Unfortunately, FTP access details you shared is not working at this end. Could you please send me working FTP/SFTP access details.

I have set the next reply to private which means only you and I have access to it.

#1167625
#1167631

Minesh
Supporter

Languages: English (English )

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

Still - the shared FTP access details not working. I also checked with my colleague for him as well the shared FTP access details not working.

Could you please send me working FTP/SFTP access details.

I have set the next reply to private which means only you and I have access to it.

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