Skip Navigation

[Resolved] Filter list is empty, should show available entries

This support ticket is created 3 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by Christian Cox 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1974629
1.png

Tell us what you are trying to do?

I have a field "assigned to" that allows a custom post to be assigned to a user on the site - the selection and data storage works, as does the display however the filter that I have setup is an empty list.

I am using the code: [wpv-control-postmeta field="wpcf-assigned-to" type="select" default_label="Assigned To" url_param="wpv-wpcf-assigned-to" class="uk-select"]

Please see attached image as example.

Because of the sensitive nature of the site I cannot give you access to it, but the code to get the users is the following:

add_filter( 'wpt_field_options', 'add_some_options_assigned_to', 10, 3);
function add_some_options_assigned_to( $options, $title, $type )
{
switch( $title )
{
case 'Assigned To':
$args = array(
'orderby' => 'display_name',
'role' => 'engineer',
'fields' => array( 'ID', 'display_name'),
);
foreach( get_users($args) as $user ) {
$user_info = get_userdata($user->ID);
$name = $user_info->user_firstname.' '.$user_info->user_lastname;
$options[] = array(
'#value' => $user->ID,
'#title' => $name
);
}
break;
}
return $options;
}

Kindest regards,

Pete

#1975077

Hello, the wpt_field_options filter is deprecated and we do not recommend using it for this very reason - it does not work with custom search. The options you add programmatically in the custom field options do not appear in custom search filters. Other users have had some luck creating their own custom shortcodes for custom search filters: https://toolset.com/forums/topic/rellenar-campo-de-tipo-select-en-filtro-de-views-usando-la-api/#post-563213

But I've found that this approach is often problematic as well, and can lead to messy URLs with duplicated URL parameters. It also doesn't work with the option "Show only available options for each input" in the View settings. So for all these reasons, I can't recommend any good solutions for using wpt_field_options with custom search Views.

For an alternative approach to connect Users and posts, check out our documentation here:
https://toolset.com/documentation/legacy-features/views-plugin/how-to-create-custom-searches-and-relationships-for-users/

#1975145

Hi Christian,

I'm sure that it was your support that helped and advised me to link the users in this way..

Would you be able to help change the code to allow it to work?

Kindest regards,

Pete

#1977489

As I said, I can't recommend any good solutions for using wpt_field_options with custom search Views. There are too many problems that don't have solutions in this approach. I will be glad to help you set it up as recommended in the documentation I linked earlier: https://toolset.com/documentation/legacy-features/views-plugin/how-to-create-custom-searches-and-relationships-for-users/

#1981713

Hi, this does not resolve my problem - the posts has an additional field "assigned to" which is automatically populated and lists the users on the website. This stores the name in the custom field above. I have a filter that should allow me to filter by this field, however it is empty.

This should not show posts with the author, it is a seperate custom field.

Hopefully this makes sense?

If I'm being an idiot here, please let me know..

Kindest regards,

Pete

#1982549

Hi, this does not resolve my problem - the posts has an additional field "assigned to" which is automatically populated and lists the users on the website. This stores the name in the custom field above. I have a filter that should allow me to filter by this field, however it is empty.
I understand you have populated a field using the wpt_field_options function, and you would like to use that field in a custom search View as a filter. However, as I've explained, this is not possible. The wpt_field_options filter is incompatible with custom search for multiple technical reasons. Other Users have tried to implement their own custom workarounds for this problem, but I can tell you that they are not 100% effective, and furthermore the wpt_field_options filter is deprecated and should be avoided as stated in our documentation: https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

Another User with a similar setup created a custom shortcode that displays a search filter with dynamically created options. You can see their work here: https://toolset.com/forums/topic/rellenar-campo-de-tipo-select-en-filtro-de-views-usando-la-api/#post-563213
However, this approach has problems and is not completely compatible with Views. For example, it does not work well with the feature "only show available options for each input". Therefore I cannot recommend this approach in good conscience, but if you understand the risks I cannot stop you from implementing something similar.

A more effective solution is to abandon the custom field populated by wpt_field_options and use a different approach as explained in the documentation I provided. The documentation shows a method you can use to replace this custom field filter with a post relationship filter. You can use a post type like "Members" to represent each User. Each User is the author of only one Member post. You will connect Members to the post type shown in your custom search View using a post relationships instead of a custom field. This solves the problem of needing to connect Users and posts, and indirectly gives you the ability to search and filter a View of posts by User associated with each post. I'll be happy to explain this in more detail and help you set this up, or help set up a static select field that includes Users as options without the wpt_field_options filter. I can show you how to export and import options into that field to set up all the initial options. In the future, you'll have to manage the options for this field manually in the field editor panel, but the result will work as expected with custom search.

Those are the two most effective workarounds I can help you implement. Unfortunately I do not have a solution that enables custom search with a custom field filter whose options are populated by the wpt_field_options filter. There is no simple solution that enables this type of dynamic custom search filter and works effectively with custom search Views for the reasons I have explained here.

#1983877

Good afternoon Christian,

Thank you so much for the detailed answer - I think I understand now, cirtainly I'm not precious about using the cpt_field_options method.

I'll have a look into the documentation, my requirements are that there is a list in the backend that is populated by users (ideally from a specific role) and then the user ID is stored in the custom field and the same list is available in the search field in the front end.

For the moment I have just added each user and ID manually into the field options (which works) - if I need to manually link an additional CPT with each user then that might just be the way to go!

Kindest regards,

Pete

#1985711

I'll have a look into the documentation, my requirements are that there is a list in the backend that is populated by users (ideally from a specific role) and then the user ID is stored in the custom field and the same list is available in the search field in the front end...For the moment I have just added each user and ID manually into the field options (which works)
Okay understood, the proxy post type solution I mentioned does fit these requirements but if a static list of Users is working sufficiently for now, that is a simpler solution to implement. Feel free to create new tickets as needed and we can discuss in more detail.