Saltar navegación

[Resuelto] Parent > child filtered loops in Bricks

This support ticket is created hace 8 meses, 3 semanas. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Este tema contiene 5 respuestas, tiene 2 mensajes.

Última actualización por Minesh hace 8 meses, 3 semanas.

Asistido por: Minesh.

Autor
Mensajes
#2691438
Screenshot 2024-04-08 at 9.51.35 a.m..jpg
Screenshot 2024-04-08 at 2.19.57 p.m..jpg

So, I am continuing my quest to swap in a Bricks loop solution for as many of my Toolset views as possible.

The current challenge involves the following one to many relationship for my online catalogue, an inventory organised by year:

enlace oculto

Parent: print-year
Child: print
Relationship: print-year_print

In views the filter is set up according to what you can see in the first screenshot.

I came across a YouTube tutorial on how to do this for ACF, both via the Bricks interface or programatically.

enlace oculto

If you watch from about 2:40 you will get the idea. He seems to be relating team memebers to portfolios. Note: he says Tim instead of Team.

According to what he outlines for the Bricks UI I am trying something similar, as shown in the second screenshot, but it doesn't work.

I am guseing this is because with Toolset relationships there is some sort of intermediary table/field invovlved. I have das a look through the database tables and I see the follwoing contenders.

wp-toolset-association > relationship_id parent_id child_id

This can aslo be done in Bricks programatically. The sample for ACF is as follows:

<?php
$current_post[] = get_queried_object_id();
return [
'post_type' => 'post-type', //e.g. faculty
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'related-field', // the ACF rel field
'value' => get_the_id(),
'compare' => 'LIKE'
)
),
'post__not_in' => $current_post,
];

For Toolset do we need to do this via some sort of join in the query?

Any insights on this appreciated

#2691502

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand you want to have way to get the related child post IDs.

What if you should try to use the Post Relationship API function toolset_get_related_posts in this case to retrieve all related posts.
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

For example:

$related_child_post_ids = toolset_get_related_posts(
                                    get_the_id(),
                                    'print-year_print',
                                     'parent',
                                     999,
                                      0,
                                      array(),
                                     'post_id',
                                     'child'
                                     );

- The above should return the related child post ID array which you can hook in to post__in argument of WP_Query.

#2691513

Ok, I have done some initial runs with this without getting it to work.

Leave it wiht me for a while and I'll see if I can work it out.

#2691517

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

ok sure.

#2691784

Ok,

I had a go at a few differnt things. Appologies in advance if my methods are a bit haphazard.

I tried a number of things including the following.

Going by the ACF example in Brick's Query Loop editor

$related_child_post_ids = toolset_get_related_posts (
get_the_id(),
'print-year_print',
'parent',
999,
0,
array(),
'post_id',
'child'
);
$current_post[] = get_queried_object_id();
return [
'post_type' => 'print', //e.g. faculty
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'related-field', // the ACF rel field
'value' => get_the_id(),
'compare' => 'LIKE'
)
),
'post__not_in' => $current_post,
];

I even tried the following:

$related_child_post_ids = toolset_get_related_posts (
get_the_id(),
'print-year_print',
'print-year',
999,
0,
array(),
'post_id',
'print'
);
$current_post[] = get_queried_object_id();
return [
'post_type' => 'print', //e.g. faculty
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'related-field', // the ACF rel field
'value' => get_the_id(),
'compare' => 'LIKE'
)
),
'post__not_in' => $current_post,
];

Each triggered a parse error in the Bricks builder.

I moved onto setting up your code example as a function in Fluent Snippets but this didn't wor out to well for me.

function GetPrintsOfYear($query) {
$related_child_post_ids = toolset_get_related_posts(
get_the_id(),
'print-year_print',
'parent',
999,
0,
array(),
'post_id',
'child'
);
return $query;
}
add_filter('pre_get_posts','GetPrintsOfYear');

Either this isn't the correct way about going about this or the hook is wrong?

#2691996

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Ok I'm afraid that we can offer you the information about how you can use our APIs and related Docs but as per our support policy we can not integrate the APIs to the third party plugins.

To get related posts you can use either: toolset_get_related_posts() function
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
OR
Using WP_Query:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

Actually you have to find a way to do that or you can contact any of our certified partners who will help you with such custom edits:
- https://toolset.com/contractors/

#2692007

Thanks Minesh,

I'll continue working through the documentation to see if I can find the solution. If all fails I can always fall back to using the templates and views I already have in place. The objective is to keep the design as much in the Bricks builder as possible, but it's not a deal breaker.