Skip Navigation

[Resolved] query related post with multiple id

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

This topic contains 5 replies, has 2 voices.

Last updated by Minesh 6 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1151281

Hi, i need to query a related post with multiple id. There is a way to do this? I use this code:

$args_macchine_brand=array(
'post_type' => 'macchina',
'order' => 'DESC',
'toolset_relationships' => array(
array(
'role' => 'child',
'related_to' => 68,
'relationship' => 'modello-macchina'
),
),
);

In "related_to" parameter i need to insert multiple id's to retrieve all chid posts of this id's.

Hope you can help.

#1151300

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - its not possible to pass multiple post IDs to related_to argument as its not supported. You need to pass the single post ID for which you want to retrieve the child posts.

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#full-documentation-of-the-query-argument

We offer another function "toolset_get_related_posts()" that does the same thing if you do not want to use WP_QUERY:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#1151304

Hi, i prefer to use WP_Query.
However, the function "toolset_get_related_posts()" can accept multiple id's?
I need to retrieve posts which have different parents.
In the previous releases of types, when the relationships were stored as meta keys, i easily achieve this.

With the new release how i can do this?

Thank you and excuse me for my bad english.

#1151311

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

However, the function "toolset_get_related_posts()" can accept multiple id's?
=> No, its not supported as you can see with the arguments available.
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

I need to retrieve posts which have different parents.
In the previous releases of types, when the relationships were stored as meta keys, i easily achieve this.
=> May be you can loop through your parent IDs.

For example:

$parent_ids = array(101, 102);
foreach($parent_ids as $k=>$v):
  // add your code here where $v contains the parent ID each time the loop iterates 
endforeach;
#1151387

Excuse me but i don't understand your solution.

Let me explain:

I have custom post type "brands".
I have custom post type "models" which is "brands" child.
I have custom post type "machines" which is "models" child but no have a direct relation to "brands".

In "single-brands.php" i need to show all models related to parent brand and all machines related to models that are children of the brand.

In previous releases of types(before version 3) this was quite easy.

How i can do this with the new version of types?

Thank you again.

#1152989

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

well - there is a limitation that WP_Query cant support such method for now.

But after consulting our Dev - this is the one of the way you can pass multiple parents using post-relationship API function toolset_get_related_posts() :

$returned_ids = toolset_get_related_posts(
    array( 'parent'=> array(111,999)),
    'relaltionship-slug',
    null,
    100,
    0,
    array(),
    'post_id',
    'child'
);

Where:
- Replace 'relationship-slug' with your original relationship slug
- Replace 111 and 999 with your parent post IDs

In addition to this, I'm also taking feature request to extend WP_Query method that supports such cases.