Skip Navigation

[Resolved] Populate a Gravity form Dropdown with post titles from a Custom post type

This thread is resolved. Here is a description of the problem and solution.

Problem:
Populate a Gravity form Dropdown with post titles from a Custom post type

Solution:
Toolset offers the post-relationship API function toolset_get_related_posts() that will allow you to fetch the related posts.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/populate-a-gravity-form-dropdown-with-post-titles-from-a-custom-post-type/#post-1775729

Relevant Documentation:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 4 years, 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 Rune Brynestad 4 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1775701
relation_to_filter_by.jpg

I'm populating a Gravity form Dropdown with post titles from a Custom post type with the slug "building".

add_filter( 'gform_pre_render_2', 'populate_posts' );
add_filter( 'gform_pre_validation_2', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_2', 'populate_posts' );
add_filter( 'gform_admin_pre_render_2', 'populate_posts' );
function populate_posts( $form ) {
 
    foreach ( $form['fields'] as $field ) {
 
        if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
            continue;
        }
 
        // you can add additional parameters here to alter the posts that are retrieved
        $posts = get_posts( 'post_type=building&numberposts=-1&post_status=publish' );
 
        $choices = array();
 
        foreach ( $posts as $post ) {
            $choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
        }
 
        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Velg eiendom';
        $field->choices = $choices;
 
    }
 
    return $form;
}

The script works fine, but I need to add another parameter to the filter to alter the posts that are retrieved:

$posts = get_posts( 'post_type=building&numberposts=-1&post_status=publish' );

I need to filter on a relationship. Only Buildings connected with the Customer Group relationship named "Eiendomsspar" should be retrieved. Is it possible?

Thanks in advice.

Kind regards
Rune

#1775729

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Toolset offers the post-relationship API function toolset_get_related_posts() that will allow you to fetch the related posts.
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

For example - To get related posts:

$posts = toolset_get_related_posts(
    get_the_ID(), //Post to query by.
    'replace-your-relationship-slug,  //Slug of the relationship to query by
    'parent', //Name of the element role to query by.
    1000, //Maximum number of returned results
    0, //Result offset
    array( //Additional query arguments
      'meta_key' => 'wpcf-customer-group',
      'meta_compare' => '=',
      'meta_value' => 'Eiendomsspar'
    ),
    'post_object', //Determines return type
    'child', // which posts from the relationship should be returned
    'title', //orderby: null, 'title', 'meta_value', 'meta_value_num'
    'ASC' // $order 'ASC' or 'DESC'.
);

Where:
- Replace "replace-your-relationship-slug" with your original post-relationship slug
- Replace "wpcf-customer-group" field slug if required with your original custom field slug
- Replace the meta_value if required.

Can you please try and check if the solution I shared help you to resolve your issue.

#1776691

My issue is resolved now. Thank you!