Skip Navigation

[Resolved] Display related post information after adding a favorite post with Favorites plugin

This thread is resolved. Here is a description of the problem and solution.

Problem: I have 3 custom post types - Artists, Books, and Works on Paper. I have two one-to-many (O2M) relationships: Artists-Books where Artist is parent and Book is child, and Artists-Works On Paper where Artist is parent and Work on Paper is child. I'm using the Favorites plugin to allow my site visitors to add favorite Books and favorite Works on Paper. In the list of favorite posts, I would like to display information about the related Artist.

Solution: The best way to handle this is to use the Favorites plugin's filters API to filter the favorites list HTML. You can use the Toolset Post Relationships API toolset_get_related_posts to get information from related posts in that filter callback:

/**
 * Toolset Support 
 * Filter the Favorites list information to display related Artiste info
 * Currently supports Livres and Oeuvres Sur Papiers post types for Favorites
 * Extendable by adding a case statement for new post type / relationship slug
 * https://toolset.com/forums/topic/find-related-custom-post-type/
 */
 
add_filter( 'favorites/list/listing/html', 'custom_favorites_listing_html', 10, 4 );
function custom_favorites_listing_html($html, $markup_template, $post_id, $list_options)
{
  // relationship slugs per favorite post type
  $post_type = get_post_type( $post_id );
  switch($post_type) {
    case "livre":
      $rel_slug = "livre_artiste";
      break;
 
    case "oeuvre-sur-papier":
      $rel_slug = "oeuvre-sur-papier-artiste";
      break;
 
    default:
      return $html;
  };
   
  // Get related posts with Toolset post relationships API
  $children_args = [
    'query_by_role' => 'parent',
    'role_to_return' => 'child'
  ];
  $children = toolset_get_related_posts( $post_id, $rel_slug, $children_args);
   
  // Build a string of related post titles, separated by a comma, 
  // and concatenate it into response HTML, then return it
  $child_titles = [];
  if( $children && count( $children ) > 0 ) {
    foreach( $children as $child ){
      $child_titles[]= get_the_title($child);
    }
    return $html . implode(',', $child_titles);
  }
   
  // in case no related posts are found, return the default HTML
  return $html;
}

Relevant Documentation:
https://favoriteposts.com/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 3 years, 9 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)

This topic contains 13 replies, has 2 voices.

Last updated by kayser 3 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1969553

Hello
hidden link
To find the name of the artist (secund line in red), I have a view with "Artists that are related to the current Livre".
For something else i need the corresponding shortcode.
How can I do this?
Thanks
Guy

#1969769

For something else i need the corresponding shortcode. How can I do this?
Hello, I am not sure I understand your question. It sounds like you want to display this View using a shortcode, is that correct? If so, you can use the wpv-view shortcode:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-view

If I have misunderstood, can you provide more details about what you want to achieve? If you would like to show me in wp-admin, I will be happy to log in and take a closer look. Please provide login credentials in the private reply fields here.

Documentation for some of the shortcodes we offer:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/

#1970933

I think you're saying you want to include the View "le nom de l'artiste" in the Single Content Listing Markup settings for the Favorites button. Unfortunately I'm not sure if there is an easy way to do this, because the View "le nom de l'artiste" is not loaded when the page loads, instead the View is loaded with AJAX after the User clicks "Ajouter ce livre ou cette oeuvre à votre liste". Views are not designed to be loaded over AJAX like this. When the View is loaded with AJAX, "the current Livre" is not defined, so the filter does not understand which Artiste post to display in the results. Is it possible to disable AJAX for this plugin and require a page reload?

#1970955

Maybe here ?
hidden link

#1970983

You may need to contact their support team to find out if it is possible to disable AJAX. I am not trained to use this plugin, so I really do not know and would prefer not to guess.

#1971015

their support is not very reactive, do you think another method is possible?

#1971119
filter-test.png

Okay I found another method. I took a few minutes to review this plugin's documentation, and I found a filter for modifying the AJAX response:
https://favoriteposts.com/

"Customize the Favorites Listing HTML"

Example filter:

add_filter( 'favorites/list/listing/html', 'custom_favorites_listing_html', 10, 4 );
function custom_favorites_listing_html($html, $markup_template, $post_id, $list_options)
{
	return $html;
}

This filter gives you access to a $post_id variable, which I found out is the ID of the current Livre post. We can use that to get the related Artiste ID using the post relationships API, or we can use it to modify the View's post relationships query filter. As a quick test, I set up a new custom code snippet in Toolset > Settings > Custom Code to fetch the Artiste post title using the post relationships API:

add_filter( 'favorites/list/listing/html', 'custom_favorites_listing_html', 10, 4 );
function custom_favorites_listing_html($html, $markup_template, $post_id, $list_options)
{
  $artistes_args = [
    'query_by_role' => 'parent',
    'role_to_return' => 'child'
  ];
  $artistes = toolset_get_related_posts( $post_id, 'livre_artiste', $artistes_args);
  $art_titles = [];
  if( $artistes && count( $artistes ) > 0 ) {
    foreach( $artistes as $artiste ){
      $art_titles[]= get_the_title($artiste);
    }
    return $html . implode(',', $art_titles);
  }
  return $html;
}

That produces the output you can see here in filter-test.png.

#1971889

Thank you Christian, this is exactly what I wanted.
I tried to include a second related post
"oeuvre-sur-papier-artiste"
Because visitors can choose books and works on paper
but I did not find the solution.
then I duplicate the function by changing only the name of the related post ?
Guy

#1972261

then I duplicate the function by changing only the name of the related post ?
No, that will not exactly work well because Ouvres sur papier are related to Artistes. Ouvres sur papier are not directly related to Livres.

These post relationships are many-to-many (M2M):
Livres Artistes
Oeuvres sur papier Artistes

So you can have multiple Artistes related to the current Livre, and you can have mulitiple Oeuvres sur papier related to each Artiste. How do you want to handle the case where the current Livre is related to multiple Artistes, and each Artiste is related to multiple Ouvres sur papier? Please explain in detail what you want to accomplish. Show me an example of this kind of output where the current Livre is related to multiple Artistes and each Artiste is related to mulitple Ouevres sur papier.

#1972275

this artist has several works on paper
hidden link
in the page there are first the books and below the works on paper
an example of a work on paper
hidden link

this book has two artists
hidden link
by Marco Chamorro
by Alice Bossut

#1972329

Yes, I understand how the Books, Artistes, and Works on Paper are set up, and I understand where to find the examples, thank you. I do not understand what you want to accomplish next. We were just modifying the Single Listing Content Markup, then you said you want to add another related post. Where do you want to add this post information - in the response after clicking the button, or somewhere else on the page?

Since each Artiste can be related to multiple Works on Paper, and each Livre can be related to multiple Artistes, how do you want to display the response when there are multiple related Artistes and multiple related Works on Paper for each artist? It would be helpful if you provide a screenshot or screenshots showing what you want to achieve.

#1972357
Capturedecran 2021-03-02 160127.jpg

what I want is that the visitors can compose a list with books and works on paper
with the name of the artist also for the works on paper,
while for the moment only the books have the name of the artist. (in the picture, look at the last choice in the list which is a work on paper and which does not have the name of the artist)

#1972707

Okay, I think I understand now. I've updated the code a bit to make it more flexible and now it works with different CPTs and different O2M post relationship slugs:

/**
 * Toolset Support 
 * Filter the Favorites list information to display related Artiste info
 * Currently supports Livres and Oeuvres Sur Papiers post types for Favorites
 * Extendable by adding a case statement for new post type / relationship slug
 * https://toolset.com/forums/topic/find-related-custom-post-type/
 */

add_filter( 'favorites/list/listing/html', 'custom_favorites_listing_html', 10, 4 );
function custom_favorites_listing_html($html, $markup_template, $post_id, $list_options)
{
  // relationship slugs per favorite post type
  $post_type = get_post_type( $post_id );
  switch($post_type) {
    case "livre":
      $rel_slug = "livre_artiste";
      break;

    case "oeuvre-sur-papier":
      $rel_slug = "oeuvre-sur-papier-artiste";
      break;

    default:
      return $html;
  };
  
  // Get related posts with Toolset post relationships API
  $children_args = [
    'query_by_role' => 'parent',
    'role_to_return' => 'child'
  ];
  $children = toolset_get_related_posts( $post_id, $rel_slug, $children_args);
  
  // Build a string of related post titles, separated by a comma, 
  // and concatenate it into response HTML, then return it
  $child_titles = [];
  if( $children && count( $children ) > 0 ) {
    foreach( $children as $child ){
      $child_titles[]= get_the_title($child);
    }
    return $html . implode(',', $child_titles);
  }
  
  // in case no related posts are found, return the default HTML
  return $html;
}

This code can be extended to support other post types, if you ever decide to add another option besides Livre and Oeuvres sur Papier. You would just copy and paste a "case" block like this:

    case "custom-post-type-slug":
      $rel_slug = "relationship-slug";
      break;
#1973173

My issue is resolved now. Thank you!