Skip Navigation

[Resolved] Filtering results by select field that is populated with custom code

This support ticket is created 5 years, 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 5 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#1234957

I am trying to: Filter results by house plan.

I have a field called "Floorplan Names Select" it is populated with the following code:

This currently grabs the names of posts defined as a floorplan and allows the user to select that floorplan with a dropdown. The dropdown is shown conditionally on posts not defined as a floorplan. This dropdown appears to work correctly

function prefix_custom_options( $options, $title, $type ){
switch( $title ){
case 'Floorplan Names':
$options = array();
$args = array (
'post_type' => array( 'listings' ), // YOUR POST TYPE
'meta_query' => array(
array(
'key' => 'wpcf-house_or_plan',
'value' => 'floorplan'
),
),
);
$query = new WP_Query( $args );
$all_posts = array_merge( $query->posts, $options );
//print_r($all_posts);
foreach ($all_posts as $row) {
$options[] = array(
'#value' => $row->post_title,
'#title' => $row->post_title
);
}
break;
}
return $options;
}

I expected to see: When creating a View Filter for the field Floorplan Names Select, a dropdown list of the values that have been assigned to the posts. The database wp_postmeta table shows the values being stored as:

5159 812 wpcf-floor-plan-names-select Floorplan 2

Instead, I got: A blank drop down when adding a view filter:

<div class="form-group">
<label>[wpml-string context="wpv-views"]Floorplan Names[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-floor-plan-names-select" url_param="wpv-wpcf-floor-plan-names-select"]
</div>

#1235002

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2019-04-24 at 14.04.36.png

The solution here rather depends on what the custom field is defined as.

If the floor-plan-names-select field is a select field then when you insert a filter control for this field the options will come from the values in the field settings (or values that you manually supply when inserting the filter). It is possible to overwrite this with a filter, but that's more complex than the alternative.

If the field is defined as a text field then when you insert the filter control you have the option to use a select dropdown with the option values coming from the postmeta data itself (see screenshot).

And that would be all you need to fix the empty select dropdown issue.

So, which is it?

#1235029

The custom field type (floor-plan-names-select) is set as a "Select" field.

In views I have tried already to display the information using "As defined by types" and as a "Select Dropdown" and the filter remains empty.

The HTML on the view / dropdown for both types shows no <options> :

<div class="form-group">
<label>Floorplan Names</label>
<select id="wpv_control_select_wpcf-floor-plan-names-select" name="wpv-wpcf-floor-plan-names-select" class="js-wpv-filter-trigger form-control"></select>
</div>

#1235033

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

If it is a select field than that's expected, because the post meta values are not an option as the source of the select dropdown.

You need to use the wpt_field_options API hook to populate the select field with the options you require, using code similar to that which you use to generate the original dropdown used to create the post meta.

See https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options