使用 Events Calendar Pro 动态填充 GF 下拉列表

Dynamically Populated GF Dropdown with Events Calendar Pro

我有一个动态填充的 GF 表单,可以从 Events Calendar Pro 中提取 CPT。我正在尝试按标题升序对事件进行排序,我还想隐藏所有过去的事件。由于我不是 PHP 开发人员,因此我进行了大量试验和错误。一段时间以来,我一直在尝试添加排序和隐藏,而此时我只是在转动我的轮子。 TIA。感谢任何帮助。

这是提取 CPT 并将其正确显示在下拉列表中的代码。

add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', '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
    // more info: http://codex.wordpress.org/Template_Tags/get_posts

    $posts = get_posts ('post_type=tribe_events&numberposts=-1' );

    $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 = 'Select an Event';
    $field->choices = $choices;

}

return $form;

}

您可以使用 tribe_get_events() 函数代替 get_posts() https://theeventscalendar.com/knowledgebase/using-tribe_get_events/

<?php
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', '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
        // more info: http://codex.wordpress.org/Template_Tags/get_posts

        // Retrieve all upcoming events
        $events = tribe_get_events( array(
            'posts_per_page' => -1,
            'start_date'     => date( 'Y-m-d H:i:s' ),
        ) );

        $choices = array();

        foreach ( $events as $event ) {
            $choices[] = array(
                'text'  => $event->post_title,
                'value' => $event->post_title
            );
        }

        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select an Event';
        $field->choices = $choices;
    }

    return $form;
}
<?php
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', '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
        // more info: http://codex.wordpress.org/Template_Tags/get_posts

        // Retrieve all upcoming events
        $events = tribe_get_events( array(
            'posts_per_page' => -1,
            'eventDisplay'   => 'list',
            'start_date'     => date( 'Y-m-d' ),
        ) );

        $choices = array();

        foreach ( $events as $event ) {
            // These two lines of code will get the event start date in the specified format
            $start_date = strtotime(tribe_get_start_date($event->ID));
            $start_date_day = date('Y-m-d', $start_date);

            $choices[] = array(
                'text'  => $event->post_title . ' ' . $start_date_day,
                'value' => $event->post_title
            );
        }

        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select an Event';
        $field->choices = $choices;
    }

    return $form;
}