Skip Navigation

[Resolved] Display related posts from Toolset via Elementor custom query ID

This support ticket is created 4 years, 4 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 9 replies, has 2 voices.

Last updated by victorT 4 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1733661

Hi

I'm trying to display related posts in Toolset from Elementor via custom query ID.

I need help to figure out how can I use the API function toolset_get_related_posts inside Elementor custom query filter to retrieve related posts (children).

Documentation from Elementor:
hidden link

I have read in a different thread that Elementor doesn't support Toolset Relationships API, is this correct?

#1734589

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2020-08-11 at 14.55.28.png

I just tried this out on a local site and I found it works.

I assume you are adding the Elementor posts widget to an Elementor template for the parent post type, and the posts widget is set to show child posts. You want to modify the query so that it only shows child posts of the currently displayed parent post, rather than all child posts.

In the Elementor post widget you need to add a Query ID, which is a string used to create a custom action that you will use to trigger your code.

In the screenshot you can see I used "ts_mod_elem_query" for my query ID.

I then added the following code snippet, which in my case is to filter child "task" posts from a "project-task" relationship based on the currently displayed parent "project".

add_action( 'elementor/query/ts_mod_elem_query  ', 'ts_mod_elem_query', 10, 2 );
function ts_mod_elem_query( $query, $widget ){

    global $post;
    $query->set( 'toolset_relationships', array(
        array(
            'role'  =>  'child',
            'related_to' => $post->ID,
            'relationship'  => 'project-task'
        )
    ) );    
}

You should be able to edit that for your needs.

#1734893

Hi Nigel

Thanks for your quick response.

Unfortunately, I have tried your code but it doesn't work.

I have also tried to follow the same code example in Elementor documentation page:

/**************************************************
 * ELEMENTOR CUSTOM QUERY ID FOR TOOLSET
 **************************************************/
add_action( 'elementor/query/ts_mod_elem_query  ', function( $query ) {

     $query->set( 'toolset_relationships', array(
        array(
            'role'  =>  'child',
            'related_to' => $post->ID,
            'relationship'  => 'receta-especial'
        )
    ) );    
});

I have also deleted brackets and tried several other ways to do this, but it won't affect the Elementor Post filter at all.

I might be doing something wrong here...

#1735519

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

In your second example you didn't set the $post object as global, so it wouldn't work.

You are sure the slug of the relationship is correct (it looks like you are connecting "receta" and "especial" post types as parent << child).

Let me set a private reply in case you want me to take a look at your site.

I'll need to know where to look for the custom code and where you are using the Posts module that this code is trying to modify.

#1735697

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Are we talking about the section "Recetas adicionales"?

This has nothing to do with the Elementor posts module and the Elementor documentation you shared, it is a Toolset View, and it looks like you already added a relationship Query Filter to that View to show recetas related to the current "especial".

So when visiting the especial post "Tortilla de Patatas" that View outputs the same receta posts which you can see connected to the especial post in the backend.

I'm not sure if I'm looking at the right thing, can you clarify what the problem is, because it looks like this is already set up correctly to me.

Con cebolla.

#1735707

Hi Nigel

I have already related posts via Toolset views as you can see in the section RECETAS RELACIONADAS.

However, this is a very clunky process as I have to copy and paste Elementor HTML code into the Toolset View which means that I have no control over any of the visuals shown on the page and that's one of the reasons the recipes on the list look terrible.

I want to show exactly the same list of related posts with an Elementor custom query so that I can apply the style and functionality that comes out of the box with Elementor easily.

Sin cebolla, por supuesto!

#1735741

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I added an Elementor post module to the template and used a version of the above code to try and modify the resulting query, but it didn't work, even though it does on my local test site.

It will be easier for me to debug this locally, so I'm taking a copy of your site and I'll update you again with my findings.

Con cebolla.

#1735791

Thanks Nigel, feel free to debug the site in your local machine, and please don't add strange ingredients to your Spanish omelette 🙂

#1735807

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

eggandchips.jpg

It worked on my local copy (though I did notice a lot of PHP notices arising from code you added to your child theme you may want to investigate), so I tried again on your live site and it is working, if you want to check.

(The template still has the View before the Posts module.)

The code is added at Toolset > Settings > Custom Code.

Sin cebolla is basically...

#1735819

Thanks for your support, Nigel.

My issue is resolved now 🙂

Huevos con patatas, mi favorito 🙂