• Resolved cncup

    (@cncup)


    Hi. Using this filter for checkboxes field:

    function wpf_checkboxes( $args, $field, $form_id ) {
    	...
    	$args[ 'include' ] = "999,200,535"; // post IDs
    	return $args;
    }
    add_filter( 'wpforms_dynamic_choice_post_type_args', 'wpf_checkboxes', 10, 3 );

    How to display the checkboxes in the given order?

    (999, 200, 535)

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Ralden Souza

    (@rsouzaam)

    Hi @cncup,

    Please know that we have a documentation for the wpforms_dynamic_choice_post_type_args filter.

    About your request, please take a look at the documentation for Order & Orderby Parameters in the WordPress Query class. The only options available are ASC and DESC. Here’s an example of ordering Dynamic Choices by title:

    add_filter( 'wpforms_dynamic_choice_post_type_args', function( $args, $field, $form_id ) {
        $args = [
            'post_type'      => $field['dynamic_post_type'],
            'orderby'        => 'title',
            'order'          => 'asc', 
        ];
        return $args;
    }, 10, 3 );

    For your custom order, this will require custom code, and I apologize as customizations like this are outside of our scope for support. In case you’d like to look into custom development options, we highly recommend using Codeable.

    Thanks!

    Thread Starter cncup

    (@cncup)

    Thanks Ralden.

    I can understand why this is outside the scope for support. It’s a bit involved and specific to one’s case.

    For future reference, I solved it with wpforms_field_properties filter. The sorting is prepared in an array beforehand without involving the form.

    add_filter( 'wpforms_field_properties', function( $properties, $field, $form_data ) {
    	// Only process for the relevant form and field. Change these values to match your form and field id's.
    	if ( !( absint( $form_data[ 'id' ] ) === 2032 && absint( $field[ 'id' ] ) === 8 ) )
    		return $properties;
    	
    	$items = [ 999=>"Peach", 200=>"Apple", 535=>"Cherry" ]; // This is created dynamically per the order logic.
    	
    	$items_count = count( $items );
    	
    	// Set items' keys and values to checkboxes.
    	if ( $items_count === count( $properties['inputs'] ) ) { // Make sure the inputs count matches the items'.
    		for ( $i = 0; $i < $items_count; $i++ ) {
    			$ids = array_keys( $items );
    			$properties[ 'inputs' ][ $i ][ 'label' ][ 'text' ] = $items[ $ids[ $i ] ];
    			$properties[ 'inputs' ][ $i ][ 'attr' ][ 'value' ] = $ids[ $i ]; // Array keys are checkbox values.
    		}
    	}
    	
    	return $properties;
    }, 10, 3 );

    You’d still need to set the $args[ ‘include’ ] in the wpforms_dynamic_choice_post_type_args filter as shown in the OP, not for the specific values but for the count. If you need it dynamic.

    You can print out the $properties object to see its structure:

    echo "<pre>" . print_r( $properties, true ) . "</pre>";

    I hope this comes in handy for someone.

    Plugin Support Ralden Souza

    (@rsouzaam)

    Hi @cncup,

    Thank you for your understanding!

    And thank you very much for the kindness of sharing the custom code. I’m sure it will be useful for other people searching for a similar solution.

    If you’d like more help with using WPForms Lite, please feel free to reach out.

    Thanks, and have a great weekend!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Sort checkboxes by given order’ is closed to new replies.