Skip to content

Instantly share code, notes, and snippets.

@zackpyle
Last active April 24, 2024 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackpyle/cd6e0805d54803b353375b36abf3f733 to your computer and use it in GitHub Desktop.
Save zackpyle/cd6e0805d54803b353375b36abf3f733 to your computer and use it in GitHub Desktop.
In Fluent Forms, set the values of a checkbox to a post type's posts
<?php
add_filter('fluentform/rendering_field_data_input_checkbox', function ($data, $form) {
if ($form->id != 1) { // Form ID
return $data;
}
// Set which field to do this on
if (\FluentForm\Framework\Helpers\ArrayHelper::get($data, 'attributes.name') != 'products') { // Field name
return $data;
}
// Query for products
$args = [
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish'
];
$query = new WP_Query($args);
$options = [];
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$options[] = [
'label' => get_the_title(),
'value' => get_the_ID()
];
}
wp_reset_postdata();
}
// Clear existing options before adding new ones
$data['settings']['advanced_options'] = [];
// Add new options
if (!empty($options)) {
$data['settings']['advanced_options'] = $options;
}
return $data;
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment