The customer created a Custom Post Type (CPT) with a date field and wanted to allow users to filter posts by month and/or year using a select list. The Toolset view filters did not support filtering by only month or year because the date field is queried as a timestamp.
Solution:
We explained that it’s not possible to filter by month or year directly using Toolset’s default date field filters. As a workaround, we suggested creating separate custom fields for the month and year, which could then be used in the filter. We provided a code example using the save_post hook to automatically populate these custom fields based on the selected date whenever a post is created or edited:
function auto_generate_month_year_from_date_field($post_id) {
// Check if it's the correct post type
if (get_post_type($post_id) == 'YOUR_POST_TYPE') {
// Retrieve the date field
$date_field = get_post_meta($post_id, 'wpcf-YOUR-CUSTOM-DATE-FIELD-SLUG', true);
if (!empty($date_field)) {
// Convert the timestamp to a DateTime object
$date_obj = DateTime::createFromFormat('U', $date_field);
// Check if date creation was successful
if ($date_obj) {
// Extract the year and month
$year = date_format($date_obj, "Y");
$month = date_format($date_obj, "m");
// Update the custom fields with year and month
update_post_meta($post_id, 'wpcf-YEAR-FIELD-SLUG', $year);
update_post_meta($post_id, 'wpcf-MONTH-FIELD-SLUG', $month);
}
}
}
}
add_action('save_post', 'auto_generate_month_year_from_date_field', 101);
Problem:
The customer wanted to display only posts with a custom image field in her view. However, when attempting to add a filter for this custom image field, it was not listed, nor were any other custom image fields.
Solution:
After further investigation, it was confirmed that the custom image fields not appearing in the filter section is expected behavior. We suggested using a wpv-conditional shortcode to test if the image exists as a workaround and provided a link to additional code that could help. Rita decided to rework the website to make images a required element when creating profiles.
When creating a new post or editing an existing post via the front end, the 'Paper content' field disappears. The issue persists even after disabling all plugins except Toolset. The problem affects both the production and staging environments.
Solution:
The issue was identified as a conflict with the "CookieYes | GDPR Cookie Consent" plugin. Disabling this plugin resolved the problem. Ensure that "CookieYes" is inactive if it is not needed, as it can cause conflicts with Toolset forms.
I have a form using AJAX without the WordPress Media Library manager and want to limit the upload file size. Despite following the documentation and various threads, my current code does not restrict file uploads to the desired size limit.
Solution:
Ensure the validation code runs only for the specific form and use the $_FILES array to check the file size correctly:
add_filter('cred_form_validate', 'my_validation', 10, 2);
function my_validation($error_fields, $form_data) {
// Ensure this code runs only for the specific form
$form_id = 12345; // Replace with your actual form ID
if ($form_data['id'] != $form_id) {
return $error_fields;
}
list($fields, $errors) = $error_fields;
// Validate 'wpcf-beleg-ausgabe' field
if (isset($_FILES['wpcf-beleg-ausgabe']) && $_FILES['wpcf-beleg-ausgabe']['size'] > 10000) {
$errors['wpcf-beleg-ausgabe'] = 'FILE SIZE TOO BIG';
}
// Validate 'wpcf-beleg-einnahme' field
if (isset($_FILES['wpcf-beleg-einnahme']) && $_FILES['wpcf-beleg-einnahme']['size'] > 10000) {
$errors['wpcf-beleg-einnahme'] = 'FILE SIZE TOO BIG';
}
return array($fields, $errors);
}