Php 使用自定义帖子类型中的自定义字段动态预填充重力表单字段

Php 使用自定义帖子类型中的自定义字段动态预填充重力表单字段,php,custom-post-type,custom-fields,gravity-forms-plugin,Php,Custom Post Type,Custom Fields,Gravity Forms Plugin,在标题中尽量具体。基本上,我有一个房地产网站,我的工作,有财产清单。属性列表是一个自定义帖子类型,并且有几个其他自定义帖子类型(每个都有自己的自定义字段)附加到它 每个属性都有一个代理(自定义帖子类型),该代理带有自定义字段,如电子邮件、facebook、电话号码等 我需要使用代理电子邮件(与作者电子邮件不同)动态地预填充重力表单上的隐藏字段,以便我可以向该地址发送电子邮件 我尝试了以下方法但没有成功,因为我确定从代理自定义帖子类型调用自定义字段时缺少了一些内容,但我不确定如何调用。这就是我目前

在标题中尽量具体。基本上,我有一个房地产网站,我的工作,有财产清单。属性列表是一个自定义帖子类型,并且有几个其他自定义帖子类型(每个都有自己的自定义字段)附加到它

每个属性都有一个代理(自定义帖子类型),该代理带有自定义字段,如电子邮件、facebook、电话号码等

我需要使用代理电子邮件(与作者电子邮件不同)动态地预填充重力表单上的隐藏字段,以便我可以向该地址发送电子邮件

我尝试了以下方法但没有成功,因为我确定从代理自定义帖子类型调用自定义字段时缺少了一些内容,但我不确定如何调用。这就是我目前正在处理的问题:

add_filter('gform_field_value_agentemail', 'populate_post_agent_email');
function populate_post_agent_email($value){

global $post;

$agents_email = get_post_meta($post->ID, 'agent_email', true);

return $agents_email;
}
我已经在重力表单字段中添加了参数名“agentemail”。如果有人知道我遗漏了什么,能够将此字段(或代理自定义帖子中的任何字段)输入此表单,将不胜感激


谢谢。

我现在正在处理这个问题-我能够将值从一个页面传递到另一个页面,并将信息附加到url的末尾-

要使其起作用,您必须在编辑相关字段时选中“允许有条件地填充此字段”

这并不是我的全部(我想在页面加载时生成它,而不是将它附加到按钮上,但这是一个开始。当我完成筛选后,我会再次发表评论)


Adam

以下是我如何使用Joshua David Nelson创建的一个小代码填充GravityForms下拉列表,josh@joshuadnelson.com

通过一些小的修改,我能够将正确的输出输出到下拉框中(正在查找用户电子邮件地址而不是用户名,但是您可以修改此脚本,通过对查询参数进行一些小的更改来输出您想要的任何内容)

要使其正常工作,您只需将上述代码添加到functions.php文件中,添加要修改的重力表单的“ID”(添加到add_filter引用中),并添加下拉字段的“Class”(其中称为“your field Class”)

如果您对上述代码有任何疑问,请告诉我


Adam

感谢Adam,我也遇到了这种方法,但不幸的是,它不适合我的需要,我还需要它来填充页面加载。如果您有其他想法,请期待回音。
// Gravity Forms User Populate, update the '1' to the ID of your form
add_filter( 'gform_pre_render_1', 'populate_user_email_list' );
function populate_user_email_list( $form ){

// Add filter to fields, populate the list
foreach( $form['fields'] as &$field ) {

// If the field is not a dropdown and not the specific class, move onto the next one
// This acts as a quick means to filter arguments until we find the one we want
    if( $field['type'] !== 'select' || strpos($field['cssClass'], 'your-field-class') === false )
        continue;

// The first, "select" option
    $choices = array( array( 'text' => 'Just Send it to the Default Email', 'value' => 'me@mysite.com' ) );

// Collect user information
// prepare arguments
$args  = array(
    // order results by user_nicename
    'orderby' => 'user_email',
    // Return the fields we desire
    'fields'  => array( 'id', 'display_name', 'user_email' ),
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );
// Get the results
$users = $wp_user_query->get_results();
//print_r( $users );

// Check for results
    if ( !empty( $users ) ) {
    foreach ( $users as $user ){
        // Make sure the user has an email address, safeguard against users can be imported without email addresses
        // Also, make sure the user is at least able to edit posts (i.e., not a subscriber). Look at: http://codex.wordpress.org/Roles_and_Capabilities for more ideas
        if( !empty( $user->user_email ) && user_can( $user->id, 'edit_posts' ) ) {
            // add users to select options
            $choices[] = array(
                'text'  => $user->user_email,
                'value' => $user->id,
            );
        }
    }
}

    $field['choices'] = $choices;

}

return $form;
}

/* end of populate advisors for dropdown field */