Skip Navigation

[Résolu] How classic View render repeatable field as post

This support ticket is created Il y a 2 années et 5 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Marqué : ,

This topic contains 7 réponses, has 2 voix.

Last updated by Waqar Il y a 2 années et 5 mois.

Assisted by: Waqar.

Auteur
Publications
#2213659
Screenshot 2021-11-05 at 5.46.02 PM.png
screencapture-smartcook-852ebiz-skill-wedge-cut-2021-11-05-17_35_24.jpeg

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.

#2213679
Screenshot 2021-11-05 at 6.01.05 PM.png

I tried to make a View and query the products by slug, I can't find the slug from the filter list.

#2213879
Screenshot 2021-11-05 at 9.11.58 PM.png

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.

#2216391

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

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

#2219133

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.

#2221221

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

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/

#2225621

Dear Waqar,

Many thanks for your reference, I will try it.

#2226843

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

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.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.