Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Doug

In-line Edit: force Taxonomy to one value

Some of my Taxonomies should have only one value. I use a plugin to set some taxonomies to one value with Radio Buttons. Is there are a way to force one value only when editing Admin Columns with In-line Editor.

3 months, 2 weeks ago
Stefan
Developer

It is possible to change the behavior with a hook. Let’s assume that you’re using our Taxonomy column with taxonomy set to ‘category’. The following snippet will change the multiple drop-down list to a single drop-down.

// Change the editing behavior for a Taxonomy columns to work with a single term selection
add_filter( 'acp/editing/view_settings', function ( $data, AC\Column $column ) {
	if ( $column instanceof \AC\Column\Taxonomy && 'category' === $column->get_taxonomy() ) {
		$data['multiple'] = false;
	}

	return $data;
}, 10, 2 );

Although the editing logic for that column was not made to work with single values, it seems to work fine after some tests.

More information and examples for the hook that was used can be found here:

https://github.com/codepress/admin-columns-hooks/blob/master/acp-editing-view_settings.php

3 months, 1 week ago
Doug

Hi Stefan,

Thanks so much, this worked. How would I get this to work with two different CPTs (‘discography’ and ‘artist’) with their respective taxonomies ‘media’ and ‘source’. Would I have to create two different code snippets or could it be handled in one code snippet?

3 months, 1 week ago
Stefan
Developer

I think that the CPT does not really matter and that you’re only interested to make it work based on the taxonomy that is used. In that case you can change the conditional in the snippet and check if the columns taxonomy is in an array of desired taxonomies.
in_array( $column->get_taxonomy(), [ 'taxonomy1', 'taxonomy2' ] )

3 months, 1 week ago
Doug

So for two taxonomies: ‘member’ and ‘source’, would the code look like this? with the && ‘category’ removed?

// Change the editing behavior for a Taxonomy columns to work with a single term selection
add_filter( 'acp/editing/view_settings', function ( $data, AC\Column $column ) {
if ( $column instanceof \AC\Column\Taxonomy && in_array( $column->get_taxonomy(), [ 'member', 'source' ] ) ) {
$data['multiple'] = false;
}
return $data;
}, 10, 2 );

3 months, 1 week ago
Stefan
Developer

I’ve changed the code for you in your own reply so that it only states the correct code there.

3 months, 1 week ago
Stefan
Developer

It is missing some extra closing parentheses.
I changed it in the snippet in your previous reply.

3 months, 1 week ago

You must be logged in to reply to this topic.