最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

forms - Sort populated GravityForms list alphabetically

programmeradmin2浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

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: 
        $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. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

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/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;
}
Share Improve this question edited Apr 2, 2019 at 5:31 phatskat 3,1741 gold badge18 silver badges26 bronze badges asked Apr 2, 2019 at 2:07 app.licious Group Pty Ltdapp.licious Group Pty Ltd 1 1
  • If you don't want to fuss with all that code, Populate Anything can handle this a lot more intuitively: gravitywiz/documentation/gravity-forms-populate-anything – Dave from Gravity Wiz Commented Apr 2, 2019 at 21:25
Add a comment  | 

1 Answer 1

Reset to default 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' );
发布评论

评论列表(0)

  1. 暂无评论