0

hope someone can help a newbie because I'm stumped on how to do this.

I've got the following code in my functions.php that populates a GravityForms form "checkbox list" of products. The user selects the products in the GF form on the website using the checkboxes. Each product that is pulled into the checkbox list is a separate WordPress Post.

I am having difficulty sorting the checkboxes alphabetically so that users can find the products easily amongst the 100+ different products(posts).

I can't find any other forum posts that look remotely similar so that I can work out how to sort it myself.

So the first part is to sort the checkboxes alphabetically and,

The second part I need is to exclude two posts that are tagged "system" or exclude those two posts because they are Categorised as "System" posts.

Much appreciated,

Andre

/** 
 * Add Gravity Forms function to dynamically populate customer 
 * letter checkboxes
 * applicious
 */
//NOTE: update the '1' to the ID of your form
add_filter( 'gform_pre_render_1', 'populate_checkbox' );
add_filter( 'gform_pre_validation_1', 'populate_checkbox' );
add_filter( 'gform_pre_submission_filter_1', 'populate_checkbox' );
add_filter( 'gform_admin_pre_render_1', 'populate_checkbox' );
function populate_checkbox( $form ) {

    foreach( $form['fields'] as &$field )  {

        //NOTE: replace 3 with your checkbox field id
        $field_id = 3;
        if ( $field->id != $field_id ) {
            continue;
        }

        // you can add additional parameters here to alter the posts that are retrieved
        // more info: http://codex.wordpress.org/Template_Tags/get_posts
        $posts = get_posts( 'numberposts=-1&post_status=publish' );

        $input_id = 1;
        foreach( $posts as $post ) {

            //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
            if ( $input_id % 10 == 0 ) {
                $input_id++;
            }

            $choices[] = array( 'text' => $post->post_title, 'value' => $post->ID );
            $inputs[] = array( 'label' => $post->post_title, 'id' => "{$field_id}.{$input_id}" );

            $input_id++;
        }

        $field->choices = $choices;
        $field->inputs = $inputs;

    }

    return $form;
}

closed as off-topic by Krzysiek Dróżdż, rudtek, Howdy_McGee Apr 4 at 14:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

I think you want to look at this line, unless I'm mistaken:

$posts = get_posts( 'numberposts=-1&post_status=publish' );

You should be able to add the orderby and order parameters:

$posts = get_posts( 'numberposts=-1&post_status=publish&order=asc&orderby=title' );

Not the answer you're looking for? Browse other questions tagged or ask your own question.