Skip to content

Instantly share code, notes, and snippets.

@infocities
Forked from hmowais/functions.php
Created May 19, 2024 23:02
Show Gist options
  • Select an option

  • Save infocities/87ef6449db4bca5de2541916ba24d483 to your computer and use it in GitHub Desktop.

Select an option

Save infocities/87ef6449db4bca5de2541916ba24d483 to your computer and use it in GitHub Desktop.
Show Custom Post Types in Gravity Form Select Field
<?php
/* Show Custom Post Types in Gravity Form Select Field */
add_filter( 'gform_pre_render_2', 'populate_cpt_titles' );
add_filter( 'gform_pre_validation_2', 'populate_cpt_titles' );
add_filter( 'gform_pre_submission_filter_2', 'populate_cpt_titles' );
add_filter( 'gform_admin_pre_render_2', 'populate_cpt_titles' );
function populate_cpt_titles( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_cpt_titles' ) === false ) {
continue;
}
$field->placeholder = 'Select Service';
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'post_title',
'post_type' => 'service', // Change this to your Custom Post Type
'post_status' => 'publish',
];
$custom_posts = get_posts( $args );
$options = [];
foreach( $custom_posts as $custom_post ) {
$options[] = ['text' => $custom_post->post_title, 'value' => $custom_post->post_title];
}
$field->choices = $options;
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment