Skip Navigation

[Resolved] no results by default until dropdown selection is made for a relationship filter

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

Problem:
The user would like to hide the results of the view when the filter is empty(no option is selected).
The user is filtering a one-to-many relationship.

Solution:
This needs custom code that will check the filter, and if no value is given, the results are emptied.
Check this reference code:

if ( $view_id === 694 ) { // the view_id
        if ( isset( $_GET['wpv-relationship-filter'] ) && ( $_GET['wpv-relationship-filter'] == 0 ) ) {
            // When no option is selected, the filter will hold 0
            $query_results->posts = array();
            $query_results->post_count = 0;
            $query_results->found_posts = 0;
        }
    }

Notice: This is for an example with one parent-child relationship. Depending on your use case, it may need to be adapted

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

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

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

Tagged: 

This topic contains 10 replies, has 2 voices.

Last updated by wabw media group 4 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1573471

Tell us what you are trying to do?
trying to have no results display as the intial result for a relationship filter in a post view

Is there any documentation that you are following?
ive tried the solution provided by nigel :
this works for view id 679 but does not work for id 694

function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){

$target_views = array(679,694); // Edit to add IDs of Views to add this to

if ( in_array( $view_id, $target_views ) ) {

// if there is a search term set
if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) {
$query_results->posts = array();
$query_results->post_count = 0;
$query_results->found_posts = 0;
}
}

return $query_results;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );
but this does not work for this filter.
Is there a similar example that we can see?
no
What is the link to your site?
the page with issue is here
hidden link

#1574095

Hello and thank you for contacting the Toolset support.

I am not sure to understand what you try to do. Can you elaborate more?

From the code provided, I would say that it empties the query results when ALL these conditions are met:
- !isset( $query_results->query['meta_query'] ) = No custom field is used in the query.
- !isset( $query_results->query['tax_query'] ) = No taxonomy is used in the query.
- !isset( $query_results->query['s'] ) = No search term is used in the query.

Without knowing more about your use case, I would be helpless.

#1574119

this code doesnt work for "Post Relationship" type filters.
did you look at the page?
what more would you like to know? this code was provided by nigel on multiple support threads and does exactly as i want on a view that filters by tax or other type filter but not on the filter i am using

so here is the page that works properly with the code provided by nigel:
hidden link
this is using an ajax search filter
[wpv-control-post-taxonomy taxonomy="faq-category" default_label="Select a Category of FAQ" type="select" url_param="wpv-faq-category"]

here is the page that doesnt work even with adding the view ID to the array in nigels code:
hidden link
this is using post control filters
[wpv-control-post-relationship ancestors="contact-list@contact-contact-list.child" url_param="wpv-relationship-filter"]
and
[wpv-control-post-ancestor type="select" ancestor_type="contact-list@contact-contact-list.child"]

when you go to the faq page nothing displays until you select it
when you go to the paralegal division contacts results display without having to select anything.

does that clear it up?

#1574145

Thank you for your feedback. I would say that one fo the conditions is not met and the code is not executed. This needs debugging to have a confirmation.

I would like to investigate a copy of your website locally and find out why the code is not run. If you agree, your next reply will be private to share a Duplicator package download link or credentials to your website to take a copy manually.
Please take a backup before sharing credentials.

#1575911

anyone working on this?

#1576883

Hello and my apologies for the late reply. I do not work on Mondays.

I took a UpdraftPlus backup of your website and I am debugging it locally. I'll get back to you as soon as I found something.

#1576943

I asked assistance from my team about this particular view/page (/contact-pages/paralegal-division-contacts/) and we were able to get what is desired without custom code.
Nigel suggested to wrap the loop content inside a conditional such as below:

[wpv-items-found]
      <!-- wpv-loop-start -->
  [wpv-conditional if="( '[wpv-search-term param='wpv_view_count']' ne '' )"]
      <!-- your loop content here -->
[/wpv-conditional]
<!-- wpv-loop-end -->
[/wpv-items-found]

And this was working for this page on my local setup.

The custom code was already only bound to another view, not "694"

$target_views = array(679); // Edit to add IDs of Views to add this to

This is the ticket where Nigel has suggested this solution https://toolset.com/es/forums/topic/mensaje-inicial-en-view/#post-1348103

Let me know if this solution is working for you.

#1577043

no this doesnt work. it doesnt load on initial page load but after changing the selection it loads even if the default option is picked.

#1577085

I also know that the code from nigel is not currently bound to 694 it didnt work so i took that view id out of the array. you have a copy you should be able to add it in the array and see that while it works for view 679 exactly as advertised it does not work for view 694 and in fact breaks the view and prevents any info from being loaded

#1577303

Hello,

I can see that the provided solution does not work after we choose a filter and then we choose none.

Please check this function on your site, it worked for me:

function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){
   
    $target_views = array(679); // Edit to add IDs of Views to add this to
	
	if ( in_array( $view_id, $target_views ) ) {
		// if there is a search term set
        if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) {
			$query_results->posts = array();
            $query_results->post_count = 0;
            $query_results->found_posts = 0;
        }
    }
	
	if ( $view_id === 694 ) { // used in "/contact-pages/paralegal-division-contacts/"
		if ( isset( $_GET['wpv-relationship-filter'] ) && ( $_GET['wpv-relationship-filter'] == 0 ) ) {
			$query_results->posts = array();
            $query_results->post_count = 0;
            $query_results->found_posts = 0;
		}
	}
   
    return $query_results;
}

Note the condition that is set for (view_id === 694).

#1577319

My issue is resolved now. Thank you!