Skip Navigation

[Resolved] Wp_query: Can 'related to' accept multiple IDs via an array?

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 2 replies, has 2 voices.

Last updated by joeC-5 1 year, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2613177

Tell us what you are trying to do?
I'm following on from a recent support request. I thought we had it working perfectly but further testing reveals a small issue in the code!

https://toolset.com/forums/topic/conditional-html-based-on-taxonomy/

The first part works fine: it gets course posts attached to the user profile. I added a "1" with array_push just in case there are no posts:

if($user_profile_posts[0]){
        $user_course_query = new WP_Query(
          array(
            'post_type' => 'corso', // "Course" post type slug
            'toolset_relationships' => array(
                'role' => 'parent',
                'related_to' => $user_profile_posts[0],
                'relationship' => 'corso-profilo-utente' // Relationship slug between "course" and "user-profile"
            ),
            'posts_per_page' => -1,
            'fields' => 'ids'
          )
        );
    $course_posts = $user_course_query->posts;
    array_push($course_posts,"1");
  }

The second part then gets subject posts that are attached to course posts from the first part. It works, but only for the first value in the array - [0]. The initial code which I copied was for a single user profile CPT, but now we have multiple courses that can be added to the profile CPT, and I want to return an array of subject posts attached to all attached course posts - not just the first one.

In the following code how can I change: 'related_to' => $course_posts[0], to accept all course posts from the code above and not just the first?

Thank you!

if($course_posts){
        $course_subject_query = new WP_Query(
          array(
            'post_type' => 'argomento', // "Subject" post type slug
            'toolset_relationships' => array(
                'role' => 'child',
                'related_to' => $course_posts[0],
                'relationship' => 'corso-argomento' // Relationship slug between "course" and "subject"
            ),
            'posts_per_page' => -1,
            'fields' => 'ids'
          )
        );
    $course_subject_posts = $course_subject_query->posts;
   
  }
#2613453

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand now, you have multiple results available for the $course_posts.

You can not pass array to the "related_to" argument but what if you loop through the $course_posts.

For example:

if($course_posts){

foreach($course_posts as $k=>$v):
$course_subject_query = new WP_Query(
          array(
            'post_type' => 'argomento', // "Subject" post type slug
            'toolset_relationships' => array(
                'role' => 'child',
                'related_to' => $v,
                'relationship' => 'corso-argomento' // Relationship slug between "course" and "subject"
            ),
            'posts_per_page' => -1,
            'fields' => 'ids'
          )
        );
    $course_subject_posts[] = $course_subject_query->posts;
endforeach;

            
  }
#2614813

Thanks so much Minesh! - it works great. I have updated the code in the following Google doc if anyone is interested in this method of content restriction:

hidden link

I also summarised the main ways to restrict content in Toolset here:

hidden link