The customer created a custom post field for images but was unable to upload AVIF format files to the field, despite AVIF images being supported elsewhere in their WordPress media library. They requested a solution to enable AVIF uploads in Toolset's custom image field.
Solution:
A code snippet was provided to extend the valid image file types for Toolset, allowing AVIF images. The customer added this code to their theme’s functions.php file or within Toolset > Settings >
function allow_avif_in_toolset( $valid_extensions ) {
// Add AVIF to the list of valid extensions
$valid_extensions[] = 'avif';
return $valid_extensions;
}
add_filter( 'toolset_valid_image_extentions', 'allow_avif_in_toolset' );
This code successfully enabled the AVIF format in the custom image field, allowing the customer to upload AVIF images as intended.
The customer wanted to display the value of the "Date of birth" user field in a custom post type called "publication." They were looking for a way to do this through a function rather than a shortcode, as publications are added via a Toolset CRED form.
Solution:
To achieve this, the customer was advised to follow a three-step process:
1- Create a Custom Field:
They needed to create a custom field in their "publication" post type to store the user's date of birth. This field could be named birth-post-field.
2- Populate Existing Posts:
A custom function was provided to loop through existing posts and retrieve the date of birth from the user meta. The code snippet provided was:
function update_birth_field_for_existing_posts() {
// Replace 'your_custom_post_type' with your actual CPT slug
$custom_post_type = 'your_custom_post_type';
// Query to get all posts of the specified custom post type
$query_args = array(
'post_type' => $custom_post_type,
'posts_per_page' => -1, // Retrieve all posts
'post_status' => 'publish', // Only published posts
'fields' => 'ids' // We only need the post IDs
);
// Execute the query
$posts = get_posts($query_args);
// Loop through each post
foreach ($posts as $post_id) {
// Get the post author ID
$author_id = get_post_field('post_author', $post_id);
// Get the 'birth' custom field value for the author
$client_birth = get_user_meta($author_id, 'wpcf-birth', true);
// Check if the author has a 'birth' value
if (!empty($client_birth)) {
// Update the post meta with the user's birth value
update_post_meta($post_id, 'wpcf-birth-post-field', sanitize_text_field($client_birth));
}
}
// Remove the action after it runs to prevent it from running multiple times
remove_action('admin_init', 'update_birth_field_for_existing_posts');
}
// Hook the function to admin_init so it runs when any admin page is accessed
add_action('admin_init', 'update_birth_field_for_existing_posts');
This code should be added to the theme's functions.php file and executed once by accessing the WordPress admin dashboard.
3- Automatically Populate on New Posts:
Another function was provided to ensure that when new publications were created or edited through the CRED form, the user's date of birth would automatically populate in the custom field:
add_action('cred_save_data', 'my_save_user_birth_to_post', 10, 2);
function my_save_user_birth_to_post($post_id, $form_data) {
// Your Toolset form ID in here
if ($form_data['id']==ID) {
// Check if the user is logged in
if (is_user_logged_in()) {
// Get the current user ID
$current_user_id = get_current_user_id();
// Get the 'birth' custom field value for the current user
$clientBirth = get_user_meta($current_user_id, 'wpcf-birth', true);
// Check if the user has a 'birth' value
if (!empty($clientBirth)) {
// Save the user's 'birth' value to the post's 'wpcf-birth-post-field' custom field
update_post_meta($post_id, 'wpcf-birth-post-field', sanitize_text_field($clientBirth));
}
}
}
}
After implementing these steps, the customer would have a new custom field linked to the user's date of birth, ensuring that new posts or edits would sync the user's birthdate with the post field. They would also be able to utilize the post field in views.
The customer experienced an issue where a numerical field was sorting incorrectly, treating numbers as strings. After fixing this, they reported a new issue: navigation problems when users are not logged in, but everything works fine when users are logged in.
Solution:
The initial sorting issue was resolved by changing the field type to a number. The new issue with navigation for logged-out users was split into a separate ticket to maintain a focus on one issue per ticket.
The customer wants to create a primary sort based on a custom field and a secondary sort based on the title (alphabetical). Despite trying various changes, the primary sort seems to override the secondary sort. The customer is unsure if the issue is related to caching or another problem.
Solution:
I initially suggested using the wpv_filter_query to modify the orderby parameter in the theme's functions.php file. This involved adding the following code, replacing '1234' with the post view's ID and 'custom-field' with the custom field slug:
The customer reported that the secondary sort was still not working. After further investigation and testing with a staging site, I identified that the issue was specific to the CPT Products.
I provided a workaround that involved modifying the orderby filter to include secondary sorting by title. The following code was added to the theme's functions.php file:
<?php
function func_orderby_title($orderby) {
global $WP_Views;
if ($WP_Views->current_view == 14670) {
$orderby = $orderby . ", wp_posts.post_title ASC";
}
return $orderby;
}
add_filter('posts_orderby', 'func_orderby_title');
?>
I tested the solution on the staging site and then replicated it on the live site, confirming that the sorting issue was resolved.