Skip Navigation

[Resolved] Hiding search results until the first search

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

Last updated by Minesh 1 year, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2626373
Search Tool After Conditional.png
Search Tool Before Conditional.png

Tell us what you are trying to do?
I'm trying to use toolset documentation to hide search results on my site until the first search. Using the link below, I'm adding the conditional block exactly as listed, using the advanced editor to add the following:

( ( '[wpv-search-term param='wpv_view_count']' ne '' ) )

"Then dragging the view loop into it. All it does is change the formatting. It does not hide the results. I've attached images of the tool before the conditional block is added and after.

Is there any documentation that you are following?
https://toolset.com/course-lesson/creating-a-custom-search/#hide-the-search-results-until-the-first-search

What is the link to your site?
hidden link

Let me know what other information would be helpful. Thank you.

#2626611

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

You must be adding the conditional to different place.

Can you please share problem URL and admin access details and let me review whats going wrong with your setup.

*** 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 have set the next reply to private which means only you and I have access to it.

#2626869
Search Tool Conditional code.png
Search Tool Conditional Placement.png

I'm not comfortable yet giving anyone admin credentials. Is there a way to provide me a test site to setup my forms in so that you can help me? I went ahead and completed the process again. See the attached images for the conditional block placement and for the conditions, per the tutorial above. Problem link is in the first post.

I can also upload the page code if that helps in the interim. Please let me know. Thank you.

#2626899

Minesh
Supporter

Languages: English (English )

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

Ok - then I've another way to offer you workaround.

To hide results on initial page load until search perform, you can use the Toolset view's hook "wpv_filter_query".
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example:
You should try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_filter('wpv_filter_query', 'func_hide_search_result_until_search_perform', 99, 3);
function func_hide_search_result_until_search_perform( $query_args, $view_settings, $view_id ) {
    
    if($view_id == 99999 ){
         if( !isset($_GET['wpv_view_count']))  {
            $query_args['post__in'] = array(0);
        }
                    
     }
    return $query_args;
}

Where:
- replace 99999 with your original view ID.

#2627167

Ok, I tried the custom code and added my view ID of 300. I'm getting an error:

"Snippet "hide-results-before-search" has been updated.
There was error when trying to re-run the snippet:Unexpected output of the code snippet: add_filter('wpv_filter_query', 'func_hide_search_result_until_search_perform', 99, 3); function func_hide_search_result_until_search_perform( $query_args, $view_settings, $view_id ) { if($view_id == 300 ){ if( !isset($_GET['wpv_view_count'])) { $query_args['post__in'] = array(0); } } return $query_args; }"

#2627217

Minesh
Supporter

Languages: English (English )

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

Can you please share screenshot how you added the code snippet to "Custom Code" section?

#2628267
Hide results custom code.png

See attached. Sorry for the delay, I didn't see this message.

#2628605

Minesh
Supporter

Languages: English (English )

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

You removed everything from your code snippet.

Can you please edit your code snippet and put the following lines before you add your code snippet:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

Just put above lines of code to your code snippet and then you should add the filter hook code I shared and save your code snippet.

#2628735
Remove duplicates custom code.png
Hide results custom code 2.png

Hello. I edited my code to add the above lines and it removed the error. However, it did not hide the results yet. Here's a screenshot of the code, as well as some other custom code I have to filter duplicates in case it's causing issues with the your code.

#2628785

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL where you added your view as well as admin access details.

*** 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 have set the next reply to private which means only you and I have access to it.

#2629025

Minesh
Supporter

Languages: English (English )

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

I see you have added the following filter code already for the same view:

add_filter( 'wpv_filter_query_post_process', 'remove_duplicates_query', 999, 3 );
 
function remove_duplicates_query( $query, $view_settings, $view_id ) {
    $relationship_slug = 'platform-tuner'; // here replace platform-tuner with many-to-many relationship slug
  
  	$viewid = 300; // here replace 105 with the view ID of your website
    if ( $view_id == $viewid && !empty( $query->posts )   ) { 
      $res = array();
      $arr = array();
      foreach($query->posts as $key => $post){
      	$post_id = $post->ID; //intermediatory post ID
        $tuner_id = toolset_get_related_posts($post_id, $relationship_slug, [
            'query_by_role' => 'intermediary', 
            'role_to_return' => 'child',
            'return' => 'post_id'
        ]); 
        if(isset($tuner_id[0]) && !in_array($tuner_id[0], $arr)){ 
        	$arr[] = $tuner_id[0];
        	$res[] = $post;
        }
      }
      $query->posts = $res;
    }
    return $query;
}

So I've adjusted the above code as given under and removed the "wpv_filter_query" hook as its not required:

add_filter( 'wpv_filter_query_post_process', 'remove_duplicates_query', 999, 3 );
 
function remove_duplicates_query( $query, $view_settings, $view_id ) {
    $relationship_slug = 'platform-tuner'; // here replace platform-tuner with many-to-many relationship slug
  
  	$viewid = 300; // here replace 105 with the view ID of your website
    if ( $view_id == $viewid && !empty( $query->posts )  and  isset($_GET['wpv_view_count']) ) { 
      $res = array();
      $arr = array();
      foreach($query->posts as $key => $post){
      	$post_id = $post->ID; //intermediatory post ID
        $tuner_id = toolset_get_related_posts($post_id, $relationship_slug, [
            'query_by_role' => 'intermediary', 
            'role_to_return' => 'child',
            'return' => 'post_id'
        ]); 
        if(isset($tuner_id[0]) && !in_array($tuner_id[0], $arr)){ 
        	$arr[] = $tuner_id[0];
        	$res[] = $post;
        }
      }
      $query->posts = $res;
    }else{

      $query->posts = array();
    }
    return $query;
}

Can you please check now and confirm it works as expected:
- hidden link

#2629075

Works absolutely perfect! I have one other request that I'll submit another ticket for. In the mean time, thank you for the help!