drvfqr5609 2018-06-19 17:09
浏览 46
已采纳

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

I have a dynamically populated GF form that pulls a CPT from Events Calendar Pro. I am trying to sort the events by ascending title and I would also like to hide all past events. I have done a lot of trial and error as I am not a PHP developer. I have been trying to add the sorting & hiding for a while and I am just spinning my wheels at this point. TIA. Any help is appreciated.

Here is the code that pulls the CPT and displays it in the dropdown correctly.

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;

}

  • 写回答

2条回答 默认 最新

  • doujin4031 2018-06-19 20:08
    关注

    You can use tribe_get_events() function instead of 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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。