Skip Navigation

[Resolved] wpv_filter_quert applys to all Views

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

Problem:

The issue here is that the wpv_filter_query function is causing the user's other views to become empty even though they had provided an ID for which view the filter should affect.

Solution:

The problem with this is that the user had placed the return statement inside the conditional statement for the view to be affected.

If the function is returning the value inside the if statement then all view will be affected and be blank.

What you need to do is to take the return statement out of the conditional statement and your views will return to normal.

A more detailed explanation can be seen here
https://toolset.com/forums/topic/wpv_filter_quert-applys-to-all-views/#post-626668

This support ticket is created 6 years, 8 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
- 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)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Pat 6 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#626085

Pat

Hello,

I'm coming back on a previews ticket (https://toolset.com/forums/topic/views-sorted-by-2-custom-fields/page/2/)
I have been able to make it work, but now, I discovered that the other Views are not working.

I have created a new Views for the "slider" postype and this Views is displaying "post" postypes!
Here is the function I placed in the function.php to get the sort on the first Views (ID 98) :

add_filter('wpv_filter_query', 'add_custom_fiels_to_view_query', 99, 3);
function add_custom_fiels_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 98 ){
return;
}

$meta_query = array();
$meta_query[] = array('key' => 'wpcf-jour-cours');
$meta_query[] = array('key' => 'wpcf-heure-debut-cours');
$meta_query[] = array('key' => 'wpcf-minute-debut-cours');
if ( !isset($query_args['meta_query']) ){
$query_args['meta_query'] = array();
}
$query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
add_filter('posts_orderby', 'custom_order');

return $query_args;
}
function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value ASC, mt1.meta_value ASC, mt2.meta_value ASC';
}

Any idea why this is impacting the other Views?

Regards
Pat

#626216

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Thank you for contacting our support forum.

The reason why this is occurring is because of this line.

if ( $view_id != 98 ){

What this line is saying is that it's going to apply the code to every view except view 98.

What you need to do is to tell the code to target the exact view you want it to apply to so you will do it like this.

if ( $view_id == 98 ){

This allows it to target only view 98.

Please let me know if this helps.
Thanks,
Shane

#626231

Pat

Hi Shane,
Thanks for your return.
Unfortunately, this is not helping a lot. I had already tested this solution and retry just now. The result is the same and my second Views is still returning "no items" due to this sorting function.
If you look at the code I sent, the if ( $view_id != 98 ){ does not refer to the function but only to a single return (in order to apply the function only to Views 98. This was the code in the ticket you sent me).

Regards
Pat

#626242

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Would you mind if I took a look at this for you ?

The private fields will be enabled for your next response. Also please let me know the page that this view is being used on.

Thanks,
Shane

#626311

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Thanks for the credentials.

Could you let me know which one of the view is having the issues as well as where is the code that does the filtering.

Thanks,
Shane

#626341

Pat

Hi Shane,

The Views that is linked to the sorting function has the ID 98. The other Views is managing the slider in the home page.
The Views ID 98 is displayed in the aaa page.
Regards
Pat

#626642

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Thanks and I took a look at this for you and it should be fine now.

Please let me know if this helps.
Thanks,
Shane

#626653

Pat

Hi Shane,

You're right, both sliders are working fine now. Can you tell me what was the issue (in order not to make it in the future !)
Thanks
Pat

#626668

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Take a look at the code below.

This is what was there.

 add_filter('wpv_filter_query', 'add_custom_fiels_to_view_query', 99, 3);
function add_custom_fiels_to_view_query($query_args, $view_settings, $view_id ) {
    if ( $view_id == 98 ){

 
       $meta_query = array();
       $meta_query[] = array('key' => 'wpcf-jour-cours');
       $meta_query[] = array('key' => 'wpcf-heure-debut-cours');
	   $meta_query[] = array('key' => 'wpcf-minute-debut-cours');
       if ( !isset($query_args['meta_query']) ){
            $query_args['meta_query'] = array();
       }
       $query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
       add_filter('posts_orderby', 'custom_order');
 return $query_args;

	}

}

This is what it should've been

 add_filter('wpv_filter_query', 'add_custom_fiels_to_view_query', 99, 3);
function add_custom_fiels_to_view_query($query_args, $view_settings, $view_id ) {
    if ( $view_id == 98 ){

 
       $meta_query = array();
       $meta_query[] = array('key' => 'wpcf-jour-cours');
       $meta_query[] = array('key' => 'wpcf-heure-debut-cours');
	   $meta_query[] = array('key' => 'wpcf-minute-debut-cours');
       if ( !isset($query_args['meta_query']) ){
            $query_args['meta_query'] = array();
       }
       $query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
       add_filter('posts_orderby', 'custom_order');

	}
 return $query_args;

}

Notice the return statement is outside of the if statement.

This is needed in the even that the view id doesn't match. The previous function wasn't returning anything hence why your other views were being affected.

This filter should return something by default.

Thanks,
Shane

#626839

Pat

Hi Shane,

Understood
Thanks for your support
Regards
Pat