Skip Navigation

[Resolved] Render related posts grouped by taxonomy when using elementor post templates

This support ticket is created 3 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by himanshuS 3 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1928487
example_project feedback.jpg
progress bar.png

I have three CPTs: 1) Criterion 2) Expert Review and, 3) Projects
Projects and Expert Review are in a O2M relationship as a project can have many expert reviews but one review can only be for one project.
Criterion and Expert Review are in a M2M Relationship as the same criterion can be used in multiple reviews and a review can use multiple criterions.
Every project and criterion has a mandatory skills taxonomy called 'skills-category'.
In a a project a person can add up to 3 skills.
When the project goes for an expert review, the criterions that have the same skill terms as the project are rendered on the expert review cred form.
When the expert submits the review with the cred form, two relationships are formed: 1) O2M between project and expert review and project, 2) M2M between expert review and criterion.

I plan to form the second relationship using build relationship function in toolset and the first relationship using the standard CRED relationship linking using url param to pass the parent post id.

I am rendering a 5 point scale using generic fields to rate each criterion. The values in the generic fields are stored in a json and eventually in a custom table i.e. I have data ready in a format like this:

[{"term-slug":"market-analysis","performance-tier-slug":"tier-3-professional","criterion-slug":"data-collection","criterion-value":"3"}]

Now, I want to show this information on the expert review post so that the criterion rating are grouped together by skill.

It gets a bit complicated as I am using elementor templates to create expert review post type.

I tried the following:
1) Create a view based on taxonomy terms related to the current post.
2) Render the elementor custom loop for criterion post type inside the view using the shortcode
3) Add the view to the expert review post type.

However, the view returns blank. What could I be missing?

I am open to other ideas if there is a simpler way to do this. I have attached a few mocks of how I would like to render the data.

#1928675

First, you should note that Elementor templates should not be used in View loops as explained in the documentation here:
https://toolset.com/course-lesson/using-toolset-with-elementor-page-builder/#limitations-when-using-elementor-and-toolset-templates-together

So I would not expect an Elementor template to work inside a View loop. Instead, I would implement basic Views and Types shortcodes inside the loop of a View first to determine whether the View is producing the correct results. For example, in a View of taxonomy terms, you can insert the wpv-taxonomy-title shortcode in the loop to confirm the View produces the correct result set.

Once confirmed, you can being nesting Views or other elements inside that taxonomy View's loop to produce nested output.

#1928727
toolset-progress-bar dynamic options.png

Christian,

Thank you for that clarification. Now, I am thinking of using a content template to render data.
My steps:
1) Create a taxonomy view for skills that are related to the current post i.e. expert review post
2) Add [wpv-taxonomy-title] to the view. I did this already and it works i.e. I can see taxonomy term names attached to a expert review post
3) I added a view below the shortcode in (2) that loops over the criterion post type and filtered based on skills-category taxonomy set by the parent view.
4) Within the view for criterion post, I want to add a content template (not assigned to any post type) that shows the name of the criterion (easy to do with wpv-post-title] and a progress bar that shows the rating saved in the json saved in the expert review custom field.

I got stuck at the last piece of showing a progress bar. I used a progress indicator element in toolset and added it to the content template. Then I clicked on dynamic insert source but could not find a custom source option to insert the rating from the json in progress bar. The current options are post id, author id and author post. Is there a way to show data from a custom shortcode?

I am happy to write the shortcode but I am not sure where to insert the data in the progress bar.

#1928765

While thinking about this further, I came up with another way to do this.

What if I enable the intermediary post type between criterion and expert review. Let's call it - rating.

Criterion <> Rating <> Expert Review.

When an expert review is saved, I store the criterion rating in the custom field on rating intermediary post type. This was I am able to add value for each criterion for each expert review in a unique field and do not need to loop through a JSON.

Now, I create a loop over intermediary posts and show the following:
- criterion title by using [wpv-post-title item=@criterion-to-expert-review.parent]
- value by using [wpv-post-field name="wpcf-criterion-rating"]

I can do all this using an elementor template.

Only problem would be to group the criterion by term or hide the term name after it has been displayed once. Suggestions to overcome this are welcome!

Does this seem feasible? If yes, I have two process questions:
1) When should I insert the value in the intermediary post. Should I use cred_save_data hook (i.e. will the intermediary post be created by then?) or should I insert data with cred_submit_complete hook?
2) When can I create a custom field for an intermediary post type? Should it be created while building an M2M relationship or can it be created later on using the custom field section in Toolset. When the relationship field is created, is it stored only in the intermediary post type or is it added to all three post type (the parent, intermediary and the child)?

#1929615
Screen Shot 2021-02-02 at 9.05.17 AM.png

Only problem would be to group the criterion by term or hide the term name after it has been displayed once. Suggestions to overcome this are welcome!
For a CSS-based solution to hide the term name after it has been displayed once, you might be able to use something like an nth-child or nth-of-type selector to set display:none for all except the first element. It depends on how your HTML is set up. More info:
https://www.w3schools.com/cssref/sel_nth-child.asp
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type

1) When should I insert the value in the intermediary post. Should I use cred_save_data hook (i.e. will the intermediary post be created by then?) or should I insert data with cred_submit_complete hook?
Actually I would use the post relationships API toolset_association_created in this case, because it covers the scenario where an association is created using Forms and also covers the scenario where the post relationship is created in wp-admin. It would be more consistent for both approaches:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

Example adding custom field value to intermediary post:

// Perform some action when an association is created in the m2m relationship 'books-chapters'
add_action( 'toolset_association_created', 'tssupp_after_association_created', 10, 5 );
function tssupp_after_association_created( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
  if( $association_slug == 'books-chapters' ) {
    // add 'some value' to custom field in intermediary post
    update_post_meta( $intermediary_id, 'wpcf-fieldslug', 'some value');
  }
}

2) When can I create a custom field for an intermediary post type? Should it be created while building an M2M relationship or can it be created later on using the custom field section in Toolset.
The field should be created when you create the M2M relationship, or you can add fields later by editing the M2M relationship (screenshot)

When the relationship field is created, is it stored only in the intermediary post type or is it added to all three post type (the parent, intermediary and the child)?
The value of a custom field applied to the M2M relationship is stored only in the intermediary post, not stored in the parent or child post.

#1929771

That worked like a charm. Thanks, Christian!

The post are getting created with their ids separated by hyphen. I was wondering if there is a way to give a custom title to the intermediary post. Something like - [Parent Post Name] For [Child Post Name].

#1929779

There's nothing in wp-admin that will allow you to specify an arbitrary intermediary post title format. You could use the same API toolset_association_created to modify the default intermediary post title with wp_update_post and get_the_title to access the related post titles:

// Perform some action when an association is created in the m2m relationship 'books-chapters'
add_action( 'toolset_association_created', 'tssupp_after_association_created', 10, 5 );
function tssupp_after_association_created( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
  if( $association_slug == 'books-chapters' ) {
    $my_post = array(
      'ID'           => $intermediary_id,
      'post_title'   => 'Intermediary title from ' . get_the_title($parent_id) . ' and ' . get_the_title($child_id)
  );
 
// Update the post into the database
  wp_update_post( $my_post );
  }
}

https://developer.wordpress.org/reference/functions/wp_update_post/
https://developer.wordpress.org/reference/functions/get_the_title/

#1930547

My issue is resolved now. Thank you!