Dear Sir/Madam,
I have repeatable field "related-tools" which is the slug of Woocommerce product. The related-tools is a custom field belong to custom post type 'class', a class may contain several related tools, how can I get the list of the repeatable field and then render them as related product post under the content template. I want to generate using View classic editor.
Best regards,
Kelvin.
I tried to make a View and query the products by slug, I can't find the slug from the filter list.
I capture a sample hidden link there are two products related to this skill, both kp57bl129 and sa209-ks are the SKU and also product slug, I want to display these two products instead of the SKU.
Hi,
Thank you for contacting us and I'd be happy to assist.
Is there any special reason you're storing post slugs and not the post IDs in the related field?
Using post ID will be more efficient in this case, because:
- filter by post ID option is available in views
- post title and slugs can be changed, but the post ID always remain the same
regards,
Waqar
Dear Waqar,
I name the slug for the WooCommerce product the same as the SKU so that customers can simply input the SKU that they are familiar with the product and easy to remember.
Thanks for writing back.
You can create a view and set it to show the product posts. And to limit the results only to the results, whose slugs are available in the "related-tools" custom field, you can add a custom function attached to the "wpv_filter_query" filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
For example:
add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
// process if specific view
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
// get the slugs from the 'related-tools' field
$related_field_values = get_post_meta( get_the_ID(), 'wpcf-related-tools' );
if(!empty($related_field_values)) {
$target_posts = array();
foreach ($related_field_values as $related_field_value) {
// get the product IDs from each found slug
$post = get_page_by_path($related_field_value, OBJECT, 'product');
if ($post) {
$target_posts[] = $post->ID;
}
}
// set the view's query to only include the related products
$query_args['post__in'] = $target_posts;
}
}
return $query_args;
}
Please replace '12345' with your actual view's ID. The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
Dear Waqar,
Many thanks for your reference, I will try it.
Glad I could help and please let me know if you have any follow-up questions.
For a new question or concern, you're welcome to start a new ticket.