Skip Navigation

[Resolved] Using Repeating Field to Load a View

This support ticket is created 2 years, 6 months ago. 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.

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)

This topic contains 2 replies, has 2 voices.

Last updated by aaronM-9 2 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2194225

Since you cannot make a relationship between two posts of the same post type, I've setup a numerical field called 'post-prerequisite' that allows multiple entries and will store the ID of the related post. In my Content Template, I'd like it to load a view for each instance of the prerequisite post ID. Can you suggest a method by which you can do this? Thanks.

- Aaron

#2194517

Waqar
Supporter

Languages: English (English )

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

Hi Aaron,

Thank you for contacting us and I'd be happy to assist.

To limit the view's results to only include the posts whose ID is saved in the repeating custom field, you can use the filter "wpv_filter_query":
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example, suppose you have a "book" post type, in which you have added a numeric repeating custom field "book-related", to store the IDs of the related book posts.

You'll create a new view (suppose the view ID is '12345') in the book post's template, that will be set to show the "Book" posts. The custom filtering function, in this case, would look like this:


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 IDs of the related posts from field "book-related"
		$related_post_id = types_render_field("book-related", array("output" => "raw", "separator" => ","));
		// if some related posts are found, set the query to include only those
		if(!empty($related_post_id)) {
			$related_post_id_arr = explode(',', $related_post_id);
			$query_args['post__in'] = $related_post_id_arr;
		}
	}
	return $query_args;
}

You can update this code snippet to use the field slug and the view ID from your website.

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.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2195115

My issue is resolved now. Thank you!

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