Last active
April 24, 2024 16:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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