Skip Navigation

[Resolved] Dropdown Filters based on custom fields not functioning properly

This support ticket is created 6 years, 5 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 3 voices.

Last updated by Beda 6 years, 4 months ago.

Assisted by: Beda.

Author
Posts
#920811

Hi,
I tried this issue on my live site and testing site and both give identical results.

I want to create a view with a custom search dropdown list to enable users to filter results based on values in a certain custom field. the custom field is a one line plane text field.

the view shows ALL posts, not just the posts that have values in the custom field.

when I choose a value from the dropdown, the results get filtered to the posts with this value.

My question is: why does the view show ALL the posts at the begining? I expect to see only the posts with this field filled.

here is the view from my live site:
hidden link

and here is a test view from my test site, based on a different custom field:

hidden link

why is the behaviour different than filtering with taxonomy? with taxonomy, it only shows the posts with the relevant taxonomy only.

So it this a glitch? or that's the expected behaviour when filtering using custom fields?

here is how the filter is setup:
hidden link

if i change it from "url paramter" to "constant", it does not show any resuts,.

thanks.

#921100

That is expected.

At the moment you load the View on the page, the results are not filtered by the Front end Search yet, but just by the Query Filters and other Views settings in the backend.

As soon you then fire the Front End Search, you will see the expected results.

What you can do to "solve" this, is to use manually entered values in the Custom Search filter, when you insert or edit the filter in the views backend, you can choose to insert the values manually in to the select Field.
I added a Screenshot of that setting.

As you see I do not use a Empty value hence that view, by default, will then return initially results with the first set value.

But this is a cumbersome solution if you have many posts and values to take care of.

On that case it's better to either use the Custom Queries in the backend only, if you do not want the user to let change the results, or, to filter the View, before you render it on the page, so to show only posts with fields that have values, and then later let the user filter by certain values.

This Requires custom code.
You would need to run a filter on wpv_filter_query:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

That would allow to filter the Posts found, just as in the code example in the DOC above, and return only posts with the field (exists).

Later, in the view you can still use a Custom Filter on the front end for that field.

If you require development help for the project regarding Custom code, I can recommend the Toolset Contractors:
https://toolset.com/contractors/

#921105

Thanks a lot Beda,

I can't see the image you attacked. can you kindly re-attach it please?

Thanks.

#921106

Hello,

Q1) My question is: why does the view show ALL the posts at the begining? I expect to see only the posts with this field filled.
Yes, this is expected result, if there is any filters, Views will display all results.
If you want to preset the dropdown menu under text "Schema Book Edition", you can pass URL parameter to the view, see below URL:
hidden link

there is an URL parameter "wpv-wpcf-schema-book-edition"

Q2) why is the behaviour different than filtering with taxonomy?
How do you setup the taxonomy filter?
in your screenshot I can see only a custom field filter:
hidden link

#921109

Hi Luo,

Q1) my problem is not that the view displays all results. my problem is that it displays posts that do NOT have any values in the targeted custom field.

Q2) there are no taxonomy in this view. I am comparing filtering using a custom field to filtering using taxonomy. in filtering using taxonomy, if a post does not have this taxonomy, it does not show.
so if i have a custom taxonomy called "cars" that has values assigned to only 30% of the posts, and I create the same view, but adjust it to display a dropdown of the cars, the posts in the inital view are only the 30% of posts that have the cars taxonomy, not all the posts.

thanks

#921111
Bildschirmfoto 2018-07-05 um 14.30.12.png

Hello

I re-attached the screenshot.

Related to your follow up comments:

1. It's expected. The View - at the moment of loading on the page - is not aware of any other filters but the ones that the user will pass later, as a Front End Custom Filter
That is the whole purpose of those Filters, to first show a set of "all" content and then a set of "filtered contents".

2. When you filter by taxonomies it's not any different.
For Example this filter will return all posts first:

<div class="form-group">
	<label>[wpml-string context="wpv-views"]Categories[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="category" type="select" url_param="wpv-category"]
</div>

If you then "use" it on the front end it will filter the results by the term chosen.

3. I tested the above on a clean install both with native and custom post types and taxonomies/fields.

Hence if your results are different, either the Plugins are out of date, there are other Codes or Plugins conflicting, or you use other settings.

4. To the solution for the reported problem - this can only be solved as described here:
https://toolset.com/forums/topic/dropdown-filters-based-on-custom-fields-not-functioning-properly/#post-921100

Please let me know if I misunderstood something, and let me know if you use other, not native Search settings, so I could try to replicate the issue you report (as in taxonomies behaving differently than fields).

Thank you!