Dynamically Generating Custom Registration Field Values from Custom Post Type List

Hi

I’ve followed this tutorial series to achieve the dynamic population of a custom registration field:

and am working with this working base code:

 add_action ("ws_plugin__s2member_before_custom_field_gen", "clubColors");
 function clubColors ($vars = array())
 	{
		$_field = &$vars["__refs"]["_field"];
		
		if($_field["id"] === "club")
			{
				$_field["options"] = 'ABC|ABC' . "\n";
				$_field["options"] .= 'HHYC|HHYC' . "\n";
				$_field["options"] .= 'HKSF|HKSF' . "\n";
				$_field["options"] .= 'HKSSA|HKSSA' . "\n";
				$_field["options"] .= 'RHKYC|RHKYC';
			}
	}

What I am trying to achieve now is, instead of hard-coding the values as above, I want to populate the Club field with dynamically pulled data from a Custom Post Type query as follows:

<?php 
$args = array (
	'post_type'              => array( 'regattaClub' ),
	'post_status'            => array( 'publish' )
);

// The Query
$clubs = new WP_Query( $args );

// The Loop
if ( $clubs->have_posts() ) {
	while ( $clubs->have_posts() ) {
		$clubs->the_post(); ?>
		<?php the_title(); ?><br />
<?php	}
} else {
	// no posts found
}

// Restore original Post Data
wp_reset_postdata();?>

I’m unsure how to marry these two blocks of code together. This is sort of where I’m at, and it’s very obvious that I have no idea what I’m doing :slight_smile: :

add_action ("ws_plugin__s2member_before_custom_field_gen", "clubColors");
 function clubColors ($vars = array())
 	{
		$_field = &$vars["__refs"]["_field"];
		
		$hkoda = array (
			'post_type' => array('regattaClub'),
			'post_status' => array ('publish')
		);
		
		$clubs = new WP_Query ($hkoda);
		
		$club = 'if ( $clubs->have_posts() ) {
			while ( $clubs->have_posts() ) {
			$clubs->the_post();
				the_title();
			}
		}';
		
		if($_field["id"] === "club")
			{
				$_field["options"] = $club . "\n";
			}
	}

Any help would be very much appreciated.

Hi @trev800 Trevor, I’m wondering if you should perhaps consult with someone who specializes in code fixes to S2Member, since this is somewhat outside the scope of this forum. Although there are many helpful people in here, it looks like it should be handled by a professional? :slight_smile:

I think @Krum does custom coding for S2Member? Forgive me if I’m wrong about that - :slight_smile:

Hi Trev.

Sorry, I’ve been super busy to help you with this one. I’m hoping someone in the community may help you out, but you can ask a developer for this customization.

Looking at your code, one error that stands out is:

		$club = 'if ( $clubs->have_posts() ) {
			while ( $clubs->have_posts() ) {
			$clubs->the_post();
				the_title();
			}
		}';

That’s not a loop, it’s a string.

Hi Trev,
Were you able to get this to work? I’ve got a similar scenario.