Skip Navigation

[Resolved] Find related post with a certain value for a custom field

This support ticket is created 2 years, 6 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 8 replies, has 2 voices.

Last updated by michielM 2 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2359737

I'm trying to find related students (leerling) for a specific location (many-to-many relationship) of whose custom field workshop-leerling-locatie equals the name of a specific workshop.

I have this, but it doesn't return any post. I would expect one result.

I've tried the meta_key without the wpcf-, but that didn't work. If I leave out the args parameter, I get all related student-locations.

$cur_students_locs = toolset_get_related_posts(
                       $locid,
                       'leerling-lokatie',
      [ 'return' => 'post_object'
        , 'query_by_role' => 'child'
        , 'role_to_return' => 'intermediary'
        , 'args' => [ 'meta_key' => 'wpcf-workshop-leerling-locatie', 'meta_value' => $workshop_name ]
      ] );

Hope you can help!

#2359779

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share details where you added the code and where you are trying to apply that code.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2359795

Minesh
Supporter

Languages: English (English )

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

Ok but could you please explain me what is your goal and why you are editing the existing code that I shared which was working fine?

#2359889

Hi Minesh, I need to test if the maximum number of students isn't exceeded for any given workshop. If it is, then the workshop won't show up and new students can't subscribe to it anymore.

Your code is still working fine, though.(I only changed the toolset_get_related_posts from legacy to the new version.)

#2360561

Minesh
Supporter

Languages: English (English )

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

Hi Minesh, I need to test if the maximum number of students isn't exceeded for any given workshop. If it is, then the workshop won't show up and new students can't subscribe to it anymore.
==>
What is the rule to check the maximum number of student limit, I mean with what to compare to check maximum student where maximum students number limit is defined?

If the limit is reached, do you want to remove that workshop from workshop dropdown?

#2360589
// Find workshops for which the max number of students have applied.
  foreach ($workshop_ids[0] as $workshop_id) {
	
  // Get the maximum as set by the administrator in the custom field "max-leerlingen" of CPT "Workshop".
    $max_students = get_post_meta($workshop_id, "wpcf-max-leerlingen", "true");

  //Only check if max_students is not null or zero.
    if ( $max_students != 0 or is_null($max_students) ) {

      //DEBUG
      print("<pre>");

  // I just figured out that the workshops are stored with their ids in the intermediary post, so this is not necessary.
//      $workshop_name = get_the_title($workshop_id);
//      print_r("max students of $workshop_name: $max_students");

// Now get the related students of a certain location through the leerling-lokatie relationship where the value of 'workshop-leerling-locatie' equals the workshop_id that we are working with here. 
      $cur_students = array();
      $cur_students = toolset_get_related_posts(
                       $locid,
                       'leerling-lokatie',
      [ 'return' => 'post_object'
        , 'query_by_role' => 'child'
        , 'role_to_return' => 'parent'
        , 'args' => [ 'meta_key' => 'wpcf-workshop-leerling-locatie', 'meta_value' => $workshop_id ]
      ] );

// Here comes the rule!
  if ( count($cur_students) >= $max_students) {
    //REMOVE WORKSHOP_ID FROM THE ARRAY (TODO)
  }

      //DEBUG
	  print("\nNumber of already registered students: " . count($cur_students_locs) . "</pre>");

    }
  }

My problem is solely with the toolset_get_related_posts function that doesn't seem to return the proper amount of students when the 'args' parameter is added. Without the statement is working correctly and gives me the expected (total) number of students.

BTW, I've tried enclosing $workshop_id in the args parameter with single quotes. Doesn't work either.

#2360601

Ah, I think I get it already. The wpcf-workshop-leerling-locatie field is a custom field of the intermediary post not of the returned student posts.

So, I need to first get all the intermediary posts with this condition and then find the related students.

#2360613

Minesh
Supporter

Languages: English (English )

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

Yes - thats correct. 🙂

#2360849

My issue is resolved now. Thank you!