Skip Navigation

[Resolved] Can't retrieve related post using API

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

This topic contains 1 reply, has 1 voice.

Last updated by diegoQ-2 3 years, 8 months ago.

Author
Posts
#2049159
Screenshot 2021-05-08 220459.jpg
Screenshot 2021-05-08 220158.jpg
Screenshot 2021-05-08 220254.jpg
Screenshot 2021-05-08 220032.jpg

Hello, thanks for any help here.

I have a website with Courses (LearnDash course) and Teachers (Toolset) post types. This is a many-to-many relationship (slug 'curso-profesor' as in the relationship edit page). Then I have a WP Gridbuilder plugin (WPGB) where I can set a filter on functions.php with the list of IDs to show. I want to include the grid in the teacher single page (created with Views) using a shortcode or WPGB block. I want to go this way (instead of using Views blocks) as the WPGB looks better and integrates WooCoomerce and LearnDash that I will require.

I want to retrieve the list of ID based in the relationship. I was following information in the these posts:

This is my site (is in stage, may be slow): hidden link

This is my code in functions.php (I didn't used the 'custome code' feature in toolset as I was unsure of how to make it work):

function my_query_args($query_args, $grid_id) {
  // grid ID 2
  if ($grid_id === 2) {
    //Get toolset related posts
    global $post;
    error_log('$post: ' . print_r($post, true));
    $related_posts = toolset_get_related_posts(
      $post->ID,
      'curso-profesor',
      array(
        'query_by_role' => 'child',
        'limit' => 999,
        'return' => 'post_id',
        'role_to_return' => 'parent' 
      ) 
    );
    error_log('related_posts: ' . print_r($related_posts, true));
    
    // filter grid with post ID only
    $query_args['post__in'] = $related_posts;
  }
  return $query_args;
}
add_filter('wp_grid_builder/grid/query_args', 'my_query_args', 10, 2);

WPGB documentation for this filter is here: hidden link

This is the debug return:

2021/05/09 02:50:26 [error] 24134#24134: *118207 FastCGI sent in stderr: "PHP message: $post: WP_Post Object
(
[ID] => 230103
[post_author] => 1
[post_date] => 2021-04-30 16:59:38
[post_date_gmt] => 2021-04-30 21:59:38
[post_content] => <!-- wp:paragraph -->
<p>Soy egresado del Instituto Popular de Cultura, donde estudié Música Tradicional y Popular de Colombia. Formé parte de la Escuela "Circo para Todos", y luego, en 2017, decidí iniciar mi formación en IKY y así complementar la práctica del Yoga y la meditación con lo aprendido tanto de la música como de la parte corporal del circo. Ahora, uno de mis grandes enfoques es el <a href="/categoria-de-curso/yoga-de-sonido/">Yoga del Sonido</a>.</p>
<!-- /wp:paragraph -->
[post_title] => Samuel Guevara Angel
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => samuel-guevara-angel
[to_ping] =>
[pinged] =>
[post_modified] => 2021-05-07 18:08:00
[post_modified_gmt] => 2021-05-...PHP message: related_posts: Array
(
[0] => 840
)" while reading upstream, client: 186.168.86.15, server: staging-ishkacoursesonline.kinsta.cloud, request: "GET /profesor/samuel-guevara-angel/?v=42983b05e2f2 HTTP/1.0", upstream: "<em><u>hidden link</u></em>:", host: "staging-ishkacoursesonline.kinsta.cloud", referrer: "<em><u>hidden link</u></em>"

Please note the $related_posts array returns empty but I have already one teacher to the course. Also, please note the WPGB plugin shows "all" courses in the grid (currently one course) on all teachers as the filter is empty.
To verify this, you can navigate on any teacher in this page hidden link . Please note only Samuel Guevara has a relationship with the course.

Thanks for any help you can provide.
Diego

#2049213

Sudden realization: When the array return empty, the grid shows all items. As I had only one course, all teachers showed the one course, in the proper teacher, because the array returned that course, in the other, because the array was empty.

There was never an issue. Sorry... Thank you!