Skip Navigation

[Resolved] SQL equivalent of toolset_get_related_posts

This support ticket is created 2 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
- 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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by collectiveU 2 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2441337

Hi,

I have the following query in PHP which I want to create via SQL.

Have tried to work backwards but unable to get it working. Any assistance would be great.

Cheers

$relationship_slug = 'event';
$child_posts = toolset_get_related_posts(
$id, // get posts connected to this one
$relationship_slug, // in this relationship
[
'query_by_role' => 'child', // origin post role
'role_to_return' => 'parent', // role of posts to return
'return' => 'post_id', // return array of IDs (post_id) or post objects (post_object)
'limit' => 999, // max number of results
'offset' => 0, // starting from
'orderby' => 'title',
'order' => 'ASC',
'need_found_rows' => false, // also return count of results
'args' => null, // for adding meta queries etc.
]
);

#2442287

Hi,

Thank you for contacting us and I'd be happy to assist.

Can you please update your code for the "toolset_get_related_posts" function like this:


// get related posts
$query_by_element = $id; // ID of post to get relationship from
$relationship = 'event'; // relationship slug
$query_by_role_name = 'child'; // $query_by_element is a child in this relation 
$limit = 999; // max number of results
$offset = 0; // starting from
$args = array(); //nothing needed
$return = 'post_id'; // We want Post IDs
$role_name_to_return = 'parent'; // We want parent.

$get_results = toolset_get_related_posts( $query_by_element, $relationship, $query_by_role_name, $limit, $offset, $args, $return, $role_name_to_return );

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2444031

Hi Waqar,

Apologies but that is not my question.

My question is how can I achieve the same result but via SQL?

Cheers

#2444737

Thanks for writing back and for clearing this further.

While a third-party plugin like 'Query Monitor' ( https://wordpress.org/plugins/query-monitor/ ), can be used to examine the exact SQL query that is taking place in the background of the "toolset_get_related_posts" function, using the SQL query directly, is not usually recommended.

Is there any special reason, you'd like to use a direct SQL query?

#2444841

We're building REST API endpoints and our DB is huge.
As a result, our API performance is quite slow and over certain number of users, our endpoints and servers are failing.

As a result, we've decided to develop an ExpressJS API solution which will directly talk to the backend DB and as a result, we want to get all the required data.

I'll review Query Monitor - thanks.

Cheesr

#2444923

Thank you for sharing this and it makes sense.

Our main concern over recommending against the direct SQL queries is future-proofing. Structural changes involving database structure are very rare and are introduced after careful consideration, with backward compatibility in mind. However, even in a very less likely event, if something is changed in the future, you'll need to update your SQL queries accordingly too.

#2447537

My issue is resolved now by using Query Monitor to get the SQL. Thank you!