Skip Navigation

[Resolved] Child post form: Selecting parent post who meets specific parent field condition

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 1 reply, has 1 voice.

Last updated by Robert Mueller 1 week ago.

Assisted by: Minesh.

Author
Posts
#2817743

I am programming a Toolset child post form in which the user shall also select the parent post of an 1:n relationship. However, due to the context only parent posts fullfulling a specific field condition (e.g. type ="C") shall be visible / selectable. The conditional group offered in the GUI only allows conditions on child fields, not on parent fields when selecting the parent post. Is there any possibility for example in the expert mode to insert some condition for the parent to show up in the selection list when setting the parent post?

Any hint would be great.
Robert

#2817882

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand - on a child post form where you have parent post select field using which you can assign the parent post, you want to display only parent post where parent post custom field type value is equal to "C". is that correct? if yes:

Can you please try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

Alternatively - you can also add the following code to your current theme's functions.php file:

function func_filter_parent_dropdown_posts_with_custom_field_value($query){
    global $post;
    
 
    if (defined('DOING_AJAX') && DOING_AJAX and isset($_REQUEST['action']) and isset($_REQUEST['slug']) and isset($_REQUEST['form_type'])    ) { 
     
        $parent_post_type = 'myparent_posttype';  /// replace with your parrent post type slug
        $relationship_slug = 'relationship-slug'; ///replace with your post-relationship slug
         
      
       if($_REQUEST['action']=='select2_potential_relationship_parents' and $_REQUEST['slug']==$relationship_slug){
              
            if($query->query['post_type'][0]==$parent_post_type ){
                $meta_query = array(   array(
                                     'key' => 'wpcf-types',
                                     'value' => 'C',
                                     'type' => 'CHAR',
                                    'compare'=> '=' )
                                );
                $query->set( 'meta_query', $meta_query );
                 
                 
            }
        }       
    }
}
add_action( 'pre_get_posts', 'func_filter_parent_dropdown_posts_with_custom_field_value', 101, 1 );

Can you please try to use the above code and check if that help you to resolve your issue.

#2818457

Thank you very much.