Skip Navigation

[Resolved] Display related posts from same CPT using repeater field

This support ticket is created 7 years, 3 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/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by patrickM-3 7 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#478354

I am trying to display related, custom posts of the same type.

The custom post type is solutions. Each solution is just a question, which is the post title, and an answer, which is the post body.

The relationship between solutions can be very random and using a taxonomy to organize those relationships would be impractical. I have added a numeric repeater field to the solutions CPT for storing the post ID of related solutions.

Now, I'm trying to figure out how to edit the view for solutions to add an unordered list of those related posts, showing the title & excerpt of each solution ID stored in the repeater field.

I know this is simple, but all of the articles I have found here discuss parent-child relationships, or using a taxonomy to build relationships. I just want to manually related posts by individual post ID, then display an unordered list of any related solutions at the bottom of each individual solution.

#478581

Dear patrick,

I assume you are using the same custom post type to store both the question and solution posts.
I suggest you setup a custom hierarchical post type, see our document:
https://toolset.com/documentation/user-guides/create-a-custom-post-type/#even-more-advanced-custom-post-types-settings
hierarchical – lets parents be specified.
and wordpress document:
https://codex.wordpress.org/Function_Reference/register_post_type#hierarchical
(boolean) (optional) Whether the post type is hierarchical (e.g. page). Allows Parent to be specified. The 'supports' parameter should contain 'page-attributes' to show the parent select box on the editor page.

With a hierarchical post type, you will be able to setup the parent/child relation ships between questions and solutions, and display the solution posts of each question post with Views plugin:
https://toolset.com/documentation/user-guides/displaying-brother-pages/
section "Create a View that lists the children of the parent page"

#478897

I am not trying to relate questions to answers - they are the same thing. A solution custom post type consists of one question with only one answer, so the single CPT of "solution" has post title as the questions and post body as the answer.

I created an additional custom field for solutions called "related solutions". It's a repeating text field. I simply store the post IDs of any other solutions that I want to relate to a particular solution.

It's not a parent-child situation, but rather just a loose relationship that allows listing any other solutions that might be similar to any single solution.

In my solution template I want to loop through any post IDs stored in the related solutions repeater field, displaying the titles and excerpts from those solutions referenced by each post ID in the related solutions repeater field.

It should be possible within the template and without having to add any additional post types, or setting up any hierarchy.

#478910

This page: https://toolset.com/documentation/user-guides/displaying-wordpress-custom-fields/repeating-fields/#Displaying Repeating Fields is missing a crucial topic.

It discusses displaying the values stored in each repetition of a repeater field and then it goes on to discuss how to display groups of repeated fields.

What is missing is the ability to display fields found by searching a post type by the value in the repeater field.

For example, lets say I have a post type of "house". What if I wanted to add a repeater field for "related houses"? These would be any houses that might be of interest to anyone looking at this house.

When I display the template for this house, I will go through the process outlined on the page above to display the rooms in this house but then, I will want to show perhaps the pictures and addresses of other houses that might be interesting to anyone looking at this house.

How do I fetch and display images and addresses of those other houses in the content template for a house?

That situation is identical to mine.

#478967

I have a CPT "Solutions" with a custom field "salesforce reference number" which is a text field. I have another custom field "related solutions" which is a repeating text field. When I display a solution, I want to show a list of the titles and excerpts of solutions who's "salesforce reference number" matches each of the values stored in the "related solutions" repeating text field.

First, I made the following content template for "Solutions":

<div>[wpv-post-body view_template='None']</div>
<div>Related Solutions:<br />
  [wpv-for-each field="related-solutions"]
      [wpv-view name="related-solutions" kbid='[types field='related-solutions'][/types]"]
  [/wpv-for-each]
</div>

Next, I made a view that will display a title and excerpt of a solution. I load this view for each value stored in the "related solutions" field and pass the current value of that field using a shortcode parameter of "kbid". My "related solutions" view filters solutions by the "salesforce reference number" custom field which is a string with value equal to VIEW-PARAM(kbid) which is set in the content template above.

Here is the "related solutions" view code:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          <div class="sol_rel_title"><a href="[wpv-post-url]" target="_blank">[wpv-post-title]</a></div>
              <div class="sol_rel_excerpt">[wpv-post-excerpt]</div>
            [/wpv-for-each]
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]

I have only been able to display each value stored in the related solutions repeating field, but I am unable to use those values to look up and display the titles, URLs and excerpts from those other solutions.

What am I doing wrong?

#479039

I assume the custom field "related-solutions" is a custom multiple instance field, which is storing the post ID of "related solutions" post.

And you are going to display those "related solutions" post information in content template "Solutions".

Since we can get the post IDs of "related solutions" post, then we can use it to display the "related solutions" post information, for example, you can try these:

1) Create a content template "single-related-solutions", with below codes:

<div class="sol_rel_title"><a href="[wpv-post-url id='']" target="_blank">[wpv-post-title]</a></div>
<div class="sol_rel_excerpt">[wpv-post-excerpt]</div>

2) edit the codes in your content template "Solutions", as below:

<div>[wpv-post-body view_template='None']</div>
<div>Related Solutions:<br />
  [wpv-for-each field="related-solutions"]
      [wpv-post-body view_template='single-related-solutions' id='[types field="related-solutions"][/types]']
  [/wpv-for-each]
</div>

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-body
attributes:
id (opt):
'$parent' | 'post-id' | '$custom post name'

#479341

This does not work. The values stored in the repeater field are not post-ids.

Each solution post has a custom field "salesforce reference number" which is a text field. That number is unique for each solution and is used by the client to identify each solution in a completely separate system that manages discussions about each solution. They need to use that value instead of post-id to relate solutions to each other because it is much more intuitive for them when they use this system.

I need to take each value in the repeater field and find the solution with that value stored in its "knowledgebase reference number" field, then display the title & excerpt. I need to repeat that for every value stored in the "related solutions" repeater field.

#479420

I got to work. Instead of using a template to view related article content I needed to use a view. In the content template for solutions, I use the wpv-for-each loop to pass each value of the related solution repeater field to a view, rather than a content template, and use that view's filter to find the correct related solution by salesforce reference number which is passed as a view shortcode attribute "sfrn".

My single solution template now has:

<div>[wpv-post-body view_template='None']</div>
<div>Related Solutions:<br />
  [wpv-for-each field="wpcf-related-solutions"]
    [wpv-view name="single-related-solution" sfrn="[types field='related-solutions'][/types]"]
  [/wpv-for-each][
</div>

My single-related-solution view filters for view attr "sfrn" and has:

<wpv-loop>
  [wpv-post-link]
   <hr />
  <div>[wpv-post-excerpt length='50' count='word']</div>
 </wpv-loop>

A special thanks to WP-Types user gianD for this post: https://toolset.com/forums/topic/wpv-for-each-not-displaying-anything/

The pitfall for me and you, Luo, was the same. Forgetting prefix the field"related-solutions" with "wpcf-" breaks the wpv-for-each loop. I am still unclear about when to use this prefix and when its not necessary.

I wonder if it's possible to modify WP-Views code so that it will handle the "wpcf-" requirement transparently, so that the for-each functions either way. Can't be too big of a bit of code to fix this and it might eliminate some tech support.

This page: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-for-each shows the wpcf- prefix being used in one example, but not the other, and there is no explanation about when it's necessary to use the prefix.

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