Skip Navigation

[Resolved] One to Many relationship limit parent selection in child cred form by parent cf

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 1 year ago.

Assisted by: Nigel.

Author
Posts
#2679438

Hello,

I had a Custom post Relation - Post A is Parent and Post X is Child
Post A (parent) has a custom field - "due date".

I have a CRED form to create Post X and I want to select only from the parent posts that meet a condition, respectively Post A with a "due date" greater than today.

Please advise me on how to achieve this.

Thank you!

#2679581

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

You will need some code to modify which parent posts are displayed in the selector on the form to publish child posts.

The code will need to use the WordPress pre_get_posts hook to modify the query arguments, meaning that the normal WP_Query parameters are available. (See the WordPress documentation for the full list of parameters: https://developer.wordpress.org/reference/classes/wp_query/#parameters).

Here is a simple example of the kind of code you need to intervene in the correct place, which you can modify for your needs for the date field:

/**
 * Intercept the query to populate the select dropdown to choose parent post in form to publish child post
 * Modify the query as required
 */
add_action( 'wp_ajax_select2_potential_relationship_parents', 'ts_mod_parent_selector', 1 );
// if guest users can use your relationship form then uncomment the following line
// add_action( 'wp_ajax_nopriv_select2_potential_relationship_parents', 'ts_mod_parent_selector', 1 );
function ts_mod_parent_selector(){

	add_action( 'pre_get_posts', 'ts_pre_get_posts_for_parent_selector' );
	function ts_pre_get_posts_for_parent_selector( $query ){
		
		// example: limit parents to those with Types custom field "active" = 1
		// note that Types custom fields use a 'wpcf-' meta key prefix
		$query->set( 'meta_key', 'wpcf-active' );
		$query->set( 'meta_value', '1' );
		$query->set( 'compare', '=' );
	}
}