Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1434929
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 questionhope 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 questionhope 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
1 Answer
Reset to default 1I 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' );
本文标签: formsSort populated GravityForms list alphabetically
版权声明:本文标题:forms - Sort populated GravityForms list alphabetically 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745637021a2667622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论