Home › Toolset Professional Support › [Resolved] How to display related posts in Elementor
Problem:
Pages are designed with Elementor. How to display related posts (connected with a Toolset relationship)?
Solution:
Use Views to query the post type of the related posts and include a post relationship Query Filter to specify the origin post. Design the output of the View to include the required fields.
Insert the View into an Elementor page using the Toolset Views Elementor widget.
Relevant Documentation:
0% of people find this useful.
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)
Tagged: Setting up post relationship, Types plugin
Related documentation:
This topic contains 13 replies, has 3 voices.
Last updated by nP-2 6 years, 3 months ago.
Assisted by: Luo Yang.
First of all, I am happy to see Elementor is supporting Toolset Custom Post Types and Fields in the new v2.1 Pro version.
I am just figuring out how to combine those two amazing tools efficiently.
I have two custom post types A and B, which are connected via the Toolset many-to-many relationship. I'm designing the Single-Template with Elementor for A and trying to output the loop for connected posts of type B within the template for A.
To achieve this, Elementor has just released a new feature for server-side filtering within the "posts"-widget (loop for any post types). hidden link
I'm wondering how I can filter the loop for B to show only connected objects.
I found some documentation for ACF: hidden link
Can you tell me which settings I need to filter the loop according to the Toolset one-to-many or many-to-many relationships function?
Thank you in advance.
Hello. Thank you for contacting the Toolset support.
Well - I think you should create a view and display related post using it.
Please check following Doc:
=> https://toolset.com/documentation/post-relationships/
Here is the Doc that explains how you can display related posts:
=> https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/
Hello Minesh,
using the view function is no option here as Elementor Page Builder is not (yet?) supported for layouting the views.
Toolset ist great for background functions (custom post types, fields, etc.) but when it comes to layout of page/single/archive templates or views a full page builder like Elementor is much more convenient.
In this case my client also insists on the Elementor page builder to edit all template parts.
Isn't there any documentation (e.g. in PHP) how to select/filter a post query to get one-to-many or many-to-many relationships?
The provided links only show how it's done in your GUI of the Views addon.
Thank you.
Well - to get the related posts using PHP you can use our post relationship API function: toolset_get_related_posts()
For example:
$results= toolset_get_related_posts( $post->ID, // the parent post / post ID for which you want to display the related post 'relationship-slug', // Slug of the relationship to query b 'parent', // 200, // the maximum number of results 0, // the offset array(), // additional query arguments 'post_object', // return format 'child', // role to return );
More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#using-the-new-post-relationships
Another way is you can still create a view using GUI and display the view using the PHP API:
=> https://toolset.com/documentation/programmer-reference/views-api/
Thank you, Minesh. I tried the following ways to query/filter the results:
Unfortunately, all those methods either deliver an empty query or fatal error (out of memory).
The name of the relationship is "projekt-team-mitglied" and the name of the intermediary post type "projekt-team-mitglie" (without d). I made sure that there is a connection between the post types I am looking at.
What else could I possibly try? Below my code and the corresponding results right after the different approaches.
// Elementor Query Hooks add_action( 'elementor_pro/posts/query/projekt_team-mitglied', function ( $wp_query, $posts_widget ) { // name of the relationship: "projekt-team-mitglied" // name of the intermediary post type: "projekt-team-mitglie" /* $wp_query->set( 'toolset_relationships', array( 'role' => 'intermediary', 'related_to' => get_the_ID(), 'relationship' => 'projekt-team-mitglied' )); // Fatal error: Uncaught InvalidArgumentException: Invalid element role name. in /####/wp-content/plugins/types/vendor/toolset/toolset-common/inc/m2m/relationship/role/role.php: // 159 Stack trace: #0 /####/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/wp_query_adjustments/m2m.php // (218): Toolset_Relationship_Role::role_from_name('other') #1 /####/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/wp_query_adjustments/m2m.php // (142): Toolset_Wp_Query_Adjustments_M2m->get_role(Array, 'role_to_query_b...', Object(Toolset_Relationship_Role_Intermediary)) // #2 /####/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/wp_query_adjustments/m2m.php // (116): Toolset_Wp_Query_Adjustments_M2m->add_single_relationship_query_where_clause(Array, Object(WP_Query)) #3 /####/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/wp in /####/wp-content/plugins/types/vendor/toolset/toolset-common/inc/m2m/relationship/role/role.php on line 159 */ /* $wp_query->set( 'toolset_relationships', array( 'role' => 'intermediary', 'related_to' => get_the_ID(), 'relationship' => 'projekt-team-mitglie' )); // WP_Query Object ( [query] => Array ( [orderby] => post_date [order] => desc [ignore_sticky_posts] => 1 [post_status] => publish [post_type] => team-mitglied [posts_per_page] => 1 [tax_query] => Array ( ) [post__not_in] => Array ( ) [paged] => 1 ) [query_vars] => Array ( [orderby] => post_date [order] => desc [ignore_sticky_posts] => 1 [post_status] => publish [post_type] => team-mitglied [posts_per_page] => 1 [tax_query] => Array ( ) [post__not_in] => Array ( ) [paged] => 1 [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [toolset_relationships] => Array ( [role] => intermediary [related_to] => 757 [relationship] => projekt-team-mitglie ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( ) [primary_table] => [primary_id_column] => ) [meta_query] => [date_query] => [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => 1 [query_vars_hash:WP_Query:private] => ee33f1544d87424c85edc0606658462d [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) ) */ /* $wp_query->set( 'toolset_relationships', array( array( 'role' => 'intermediary', 'related_to' => get_the_ID(), 'relationship' => 'projekt-team-mitglie' ) )); // WP_Query Object ( [query] => Array ( [orderby] => post_date [order] => desc [ignore_sticky_posts] => 1 [post_status] => publish [post_type] => team-mitglied [posts_per_page] => 1 [tax_query] => Array ( ) [post__not_in] => Array ( ) [paged] => 1 ) [query_vars] => Array ( [orderby] => post_date [order] => desc [ignore_sticky_posts] => 1 [post_status] => publish [post_type] => team-mitglied [posts_per_page] => 1 [tax_query] => Array ( ) [post__not_in] => Array ( ) [paged] => 1 [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [toolset_relationships] => Array ( [0] => Array ( [role] => intermediary [related_to] => 757 [relationship] => projekt-team-mitglie ) ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( ) [primary_table] => [primary_id_column] => ) [meta_query] => [date_query] => [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => 1 [query_vars_hash:WP_Query:private] => ee33f1544d87424c85edc0606658462d [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) ) */ /* $wp_query = toolset_get_related_posts( get_the_ID(), // get posts related to this one 'projekt-team-mitglied', // relationship between the posts 'intermediary', 200, 0, // pagination array(), 'post_object', 'child' ); // array() --> empty */ /* $wp_query = get_view_query_results( 7435 ); // result // Fatal error: Out of memory (allocated 482869248) (tried to allocate 266240 bytes) in /####/wp-content/plugins/wp-views/embedded/inc/wpv.class.php on line 1419 // Fatal error: Out of memory (allocated 482869248) (tried to allocate 307200 bytes) in /####/wp-includes/wp-db.php on line 1924 */ /* $wp_query = new WP_Query( array( 'post_type' => 'team-mitglied', 'numberposts' => 3, 'toolset_relationships' => array( array( 'role' => 'intermediary', 'related_to' => get_the_ID(), 'relationship' => 'projekt-team-mitglied' ) ), ) ); // Fatal error: Out of memory (allocated 482869248) (tried to allocate 262144 bytes) in /####/wp-includes/class-wp-query.php on line 3230 */ print("ID: " . get_the_ID()); print_r($wp_query); }, 10 ,2 );
Hello,
Minesh isn't available, I will take care this thread.
Since it is a custom codes + Comapaibility problem with Elementor, please provide a copy of your website in below "private detail box"
https://toolset.com/faq/provide-supporters-copy-site/
also point out the problem page URL and where I can edit your PHP codes, describe detail steps to see the problem:
Unfortunately, all those methods either deliver an empty query or fatal error (out of memory).
I need to test and debug it in my localhost.
I have requested access the file and download it, please grant the request. thanks
Hi Luo, I granted the request yesterday. Have you been able to download the file?
I am downloading the file, trying to duplicate the same problem, will feedback if there is anything found
I have tried to install the duplicator package in my localhost, but get a timeout error, and I have tried to import the database dump file database.sql, there are some data missing, for example, tables wp_user and wp_usermeta are empty, so the file database.sql is broken, can not use it to debug
Please provide the database dump file in your google drive disk
https://toolset.com/forums/topic/query-related-posts-with-elementor-query-id-function/#post-950505
and update this thread. thanks
I just created a sql export via phpMyAdmin and shared it within the google drive disk. I hope this works out. In case you see a password protected site, please just remove the password protection plugin in the plugins directory.
Well - Luo is on Vacation. This is Minesh here and I'll take care of this ticket. Hope this is Ok.
Well - I tried to access the google drive but I do not have permission to access it.
It will be great and to speed up the things and to have 1:1 copy of your site so I do not miss anything to have duplicator copy of your site.
Could you please send me duplicator copy of your site: https://toolset.com/faq/provide-supporters-copy-site/
I have set the next reply to private which means only you and I have access to it.
I have tried your duplicator package again, the duplicator plugin does not work, but I can setup the site manually.
You are using a wordpress multisite installation, I assume we are talking about the main site, the site ID is 1, I have tried the URL you mentioned above:
hidden link but get a 404 error
then I checked it in back-end, there isn't any post is using title like "eta-fabrik", see screentshot: Projekte,JPG
So I have tested the post "Poolregler" in front-end, it display only the post title, see screenshot Poolregler.JPG
I don't see the section you mentioned "right under Ansprechpartner", so your duplicator package does not duplicate the problem, it can not be used to debug the PHP codes, please provide a live website with the same problem, fill below " private detail box" with credentials and FTP access, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug your PHP codes.
After all those problems I tried to figure out a different solution and switched back to the view solution with the newly released Toolset View Widget for Elementor.
So I created a dynamic Elementor template for the loop view and pasted the shortcode into a Toolset view. I used the Toolset view functionality to filter the relationships and then added the view to my Elementor page template with the Toolset view widget.
Even tough this is not very intuitive and quite complicated, it works for now. As recently one could hear that Toolset and Elementor are working together on improving the compatibility, let's hope there will be a smarter solution for this in the near future (e.g. an option for adding Elementor content within Toolset Views).
Thanks for your help.