Skip Navigation

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

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 hace 4 años, 2 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por Rune Brynestad hace 4 años, 2 meses.

Asistido por: Minesh.

Autor
Mensajes
#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

Idiomas: Inglés (English )

Zona horaria: 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!