Skip Navigation

[Resolved] Fields options registered with wpt_field_options are not shown in the Custom Searches.

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

Problem:
When I create fields options with wpt_field_options, then those options, even if saved against a post, are not shown in the Views Custom Search Filter.

Solution:
There is no solution without custom code as shown here

There are however feature requests for a more elaborated API in the progress. But we cannot state any ETA on this.
If you wish to add your vote to the request, please leave a comment here.

100% of people find this useful.

This support ticket is created 7 years, 3 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
- - 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 8 replies, has 2 voices.

Last updated by Beda 7 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#561503
Screenshot_5.png
Screenshot_4.png
Screenshot_3.png
Screenshot_2.png

Buenos días,

tengo una duda sobre como rellenar un campo select en el filtro de una view y me gustaría saber si pueden ayudarme.

En el sistema que estoy desarrollando tengo un CPT llamado Envios que entre otros campos tiene un select que relleno desde PHP usando la API con el siguiente código:

add_filter( 'wpt_field_options', 'jhm_populate_options', 10, 3);
function jhm_populate_options( $options, $title, $type ) {
switch ( $title ){
case 'Kiidriver':
$options = array();

$options[] = array(
'#value' => '',
'#title' => '',
);

$user_query = new WP_User_Query( array( 'role' => 'kiidriver' ) );
$kiidrivers = $user_query->get_results();

foreach ($kiidrivers as $driver) {
$options[] = array(
'#value' => $driver->display_name,
'#title' => get_user_meta( $driver->ID, 'first_name', true ) . ' ' . get_user_meta( $driver->ID, 'last_name', true ),
);
}

break;
}

return $options;
}

Esto me funciona perfectamente tanto para el formulario CRED de agregar Envio como para el de edición.

El problema lo tengo con el select en el filtro de la vista que lista los Envios dados de alta, no se como rellenarlo desde PHP usando la API de la misma manera que hago en los formulario CRED.

Muchas gracias por su tiempo.
Un saludo.

#561513

Infelizmente la API wpt_field_options no es una API publicada.

Necesito antes consultar con los desarroladores si nosotros podemos asistir esta API y en caso que si, debemos tambine documentarla.

Ahora, si tu haces unos Posts con unas opciones qualquieras, estas deben se guardar en la base de datos, corecto?
Entonces, la opcion ya existe cuando usas la View, dato que esta View solo puede presentar datos ya guardados en la base de datos.

Entonces no necesitas esto filtro, las opciones deben aparecer porque ya existen en la base de datos.

Si esto no es el caso significa que los valores no se guardan, y entonces no hay como hacer una Query por estos valores.

#561534

Hi Beda,

i try to write in english 🙂

The field values are stored in database correctly, as you can see in screenshot-4.png, when i add one Envio (my CPT) the select field Kiidriver is populated without issues from the PHP function.

In the screenshot-5.png image all the metafields from the CPT are showed correctly to edit (Kiidriver too).

But as you can see in the screenshot-3.png image, in the view the select to filter the results isn´t populated as in the add and edit form.

I need all the select (the select in the add form, the select in the edit form and the select in the filter) be populated with the values from the PHP function.

If this way to do it isn´t correct i would like to know how can i do this?

Regards.

#562236

Let me resume:

1. With the filter, you add options to the field. Those are not yet field values
2. To become actual field values, you need to save a post with such a selected option.
3. After, in any View, every search field or even the Query Filter will let you use that value.

Of course, first you need to save a post with that particular value.

Have you done that? Or do you mean that the option should appear in the View even before you saved a post with it?
That will not work.

If instead all above steps are done, I do not see why the option should appear but not be saved.

In that case, I need to know:
- what happens when you do this in the backend?
- could you try to get the information passed by the CRED form for this field in the $_POST variable?
This will tell us if the information is even there.

In case these details are not available, I think the best is let me know step by step what you do on your site (starting with how/where you insert the function and ending with where you would now expect the value)
I can then carefully try to replicate this and see where it hiccups.

If it's easier, you can also submit me a Site's Snapshot and elaborate how to use it so to replicate the issue.
https://toolset.com/faq/provide-supporters-copy-site/

#562344

I see what you try to do.

1. This is not how it's intended to work.
You should populate the Fields in the Fields GUI when you add the Single Fields, you also should add the options.
2. These are complex fields and seem to reject the way you store the data.
3. When you remove the Custom Code and add the options in the Types GUI as it's intended this works fine.

Now, since the hook you use is not publicly documented, I need first to ask the Developers if we can assist this problem.

Thank you for the patience meanwhile

BTW; I am able to replicate this, but the problem is that it uses a non-documented feature, hence I need first more informations to proceed.

#562357

Ok thanks, i'll wait for your response.

I can not populate the select in the in the Fields GUI when i add the Single Field as you say in the first point of your answer because the select content is not static and it depends on the returned wp_query values wrote in PHP function.

The use of this hook is the solution i found after search in the Toolset forums, maybe is not the correct way to do it, but i don't found another one.

We'll wait the developers answer 🙂

#563132

We do not have any hook that allows you to add dynamically some options to the Custom Filter in a View.

Hence, the only solution is to add them manually or create your own select Search Field and link it to the correct URL's with the Correct URL parameters.

I escalated this as an important feature to be added as soon as possible, to either update the options table when options are added to the field (that is the real core cause) or to add a filter to the Views Search where we can hook in.

I will update you here.

#563213

Ok, Beda, thanks

meanwhile i explain you how i do it, perhaps may be useful for other people.

I have wrote a shortcode which return the select HTML code with the same name, classes and id that have the original HTML from Toolset, but mine has been populated. In the views instead of Toolset shortcode i put my shortcode.

The code of the shortcode:

// Add shortcode for field in view populated from PHP code
add_shortcode( 'select-kiidrivers', 'jhm_select_kiidrivers_shortcode' );
function jhm_select_kiidrivers_shortcode() {
	// WP Query
	$args = array(
		'post_type'	=> 'envio',
		'meta_key'	=> 'wpcf-kiidriver-envios',
	);
	$query = new WP_Query( $args );

	// Put the founded values into the array
	$kiidrivers_list = array();
	foreach ( $query->posts as $post ) {
		$kiidriver = get_post_meta( $post->ID, 'wpcf-kiidriver-envios', true );

		if ( $kiidriver == '' ) continue;

		if ( !in_array( $kiidriver, $kiidrivers_list ) ) {
			$kiidrivers_list[] = $kiidriver;
		}
	}

	// Sort the results alfabetically
	sort( $kiidrivers_list, SORT_STRING );

	// Get the selected parameter if exists
	$selected = $_GET['wpv-wpcf-kiidriver-envios'];

	// Generate the shortcode output
	$str = '<select id="wpv_control_select_wpcf-kiidriver-envios" name="wpv-wpcf-kiidriver-envios" class="js-wpv-filter-trigger form-control">';
	$str .= '<option value=""></option>';

	foreach ( $kiidrivers_list as $kiidriver ) {
		if ( $kiidriver == $selected ) {
			$str .= '<option value="' . $kiidriver . '" selected="selected">' . $kiidriver . '</option>';
		} else {
			$str .= '<option value="' . $kiidriver . '">' . $kiidriver . '</option>';
		}
	}

	$str .= '</select>';

	return $str;
}
#564593

It is not possible currently to programmatically pass this new set of options to a View Custom Search.

You will need to populate the Search manually or with the Custom Code that creates your own select field.

We added a feature request to add a filter that can be used in all 3 plugins (Views, CRED and Types).

This will require some time to be implemented.