Dear Sir/Madam,
I want to return nothing if there is no parameter "cmid" passed, I refer to the solution from https://toolset.com/forums/topic/display-only-filtered-values/ but I can't do the same.
Here is the code
if ($_SERVER['REMOTE_ADDR'] == '14.198.104.144') {
add_filter( 'wpv_filter_query', 'fn_show_empty_result', 99, 3 );
}
function fn_show_empty_result( $query_args, $setting, $view_id ) {
if ($view_id == 2181) {
if( isset($_GET['cmid']) && $_GET['cmid'] == '' ) {
$query_args['post__in'] = array(0);
}
}
return $query_args;
}
It shows all result without filtering if there is no cmid parameter included in URL, what is the problem to my code?
Best regadrs,
Kelvin.
Hello Kelvin and thank you for contacting the Toolset support.
To better assist you with this issue, I'll need to take a closer look at the view in the frontend to check if the URL parameter is 'cmid', the view_id is 2181.
But, I wonder why using the filter on condition:
if ($_SERVER['REMOTE_ADDR'] == '14.198.104.144') {
add_filter( 'wpv_filter_query', 'fn_show_empty_result', 99, 3 );
}
Would you allow me temporary access to check this further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Dear Jamal,
I solved my issue by changing the code with below code
function fn_show_empty_result( $query_args, $setting, $view_id ) {
if (in_array($view_id, array(2181, 2006))) {
if(!isset($_GET['appid']) || $_GET['appid'] == '' ) {
$query_args['post__in'] = array(0);
}
}
return $query_args;
}
I change the cmid to appid for better variable naming.
I use
$_SERVER['REMOTE_ADDR'] == '14.198.104.144')
because I want to do some debug but I am not sure whether wpv_filter_query is the best filter for my case. Please advise.
Best regards,
Kelvin.
Hello Kelvin,
I am glad you fixed the custom code.
The wpv_filter_query allows to customize the view's query. If you use it inside that condition, it will only be applied when the condition is met.
But, I am not sure what debugging you are talking about. Keep in mind that Toolset Views do have a debugging mode that generates a popup, only for administrator users, with more details about the views, content templates, or archive templates that are used in the currently visited page. Check this article's section https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/#the-views-debug-tool
I hope this helps. Let me know if you have any questions.