页面从数组返回ID,如何在acf wordpress中返回正确的post值


Page returning ID from array, how to return the correct values for post in acf wordpress

我在使用关系字段返回正确的值时遇到了一点麻烦。

我的最终结果是使用关系字段将选定的位置添加到重力表单中的下拉列表中。我可以填充下拉菜单,并使其大部分工作。

问题是当我调用function .php内的字段并将结果打印到我的页面时,我所得到的是正确的帖子ID,但get_the_title()返回页面本身的页面名称而不是我之后的帖子。

即使在打印出来的数组中,我也看不到实际的post name (location name)。

任何帮助都非常感谢。

过去几天一直在寻找解决方案,但没有运气。

这是我写的代码。

add_filter( 'gform_pre_render_4', 'freetrial_studios' );
add_filter( 'gform_pre_validation_4', 'freetrial_studios' );
add_filter( 'gform_pre_submission_filter_4', 'freetrial_studios' );
add_filter( 'gform_admin_pre_render_4', 'freetrial_studios' );
function freetrial_studios( $form ) {
foreach ( $form['fields'] as &$field ) {
    if ( $field->type != 'select' || strpos( $field->cssClass, 'studio-list' ) === false ) {
        continue;
    }
    // Get the ID of post
    $studio_id = get_the_ID(); 
    $studios = get_field('field_557a974776dd2', $studio_id);

    if( $studios ) {
        $studio_title = get_the_title( $studio_id );    
        $studio_meta = get_post_custom($studio_id);     
        $choices = array();

        $locations = array();
        $p = get_post(); // gets the basic post details
        $f = get_fields(); // gets the ACF details
        $m = array_merge((array)$p, (array)$f); // merges all details into one array
        array_push($locations, $m);

        foreach ( $studios as $studio ) {
            echo '<pre>';
                print_r( get_field('field_557a974776dd2', $studio_id, false) );
                echo '....';
                print_r( get_the_title( $studio_id ) );     
                echo '....';
                print_r( the_title( $studio_id ) );     
                echo '....';
                print_r( $studio_meta );    
                echo '....';    
                print_r( $m );  
                echo '....';                        
            echo '</pre>';
            // Populate the drop down field with values                         
            $choices[] = array( 'text' => $studio, 'value' => $studio );
            $field->choices = $choices;   
        }
        $field->placeholder = 'Select a Studio';
        $field->choices = $choices;        
    }
}
return $form;
}

下面是打印的输出。

Array
 (
  [0] => 102
  )
  ....Free trial form 2....113Free trial form 2....Array
 (
[_edit_lock] => Array
    (
        [0] => 1437533210:2
    )
[_edit_last] => Array
    (
        [0] => 2
    )
[_wp_page_template] => Array
    (
        [0] => default
    )
[studio_list] => Array
    (
        [0] => a:1:{i:0;s:3:"102";}
    )
[_studio_list] => Array
    (
        [0] => field_557a974776dd2
    )
[email_address] => Array
    (
        [0] => email@gmail.com
    )
[_email_address] => Array
    (
        [0] => field_55add0bd111c1
    )
 )
 ....Array
(
[ID] => 113
[post_author] => 2
[post_date] => 2015-07-21 14:53:03
[post_date_gmt] => 2015-07-21 04:53:03
[post_content] => Form id 4
[gravityform id="4" title="false" description="false" ajax="true"]
[post_title] => Free trial form 2
[post_excerpt] => 
[post_status] => publish
[comment_status] => open
[ping_status] => closed
[post_password] => 
[post_name] => form-2
[to_ping] => 
[pinged] => 
[post_modified] => 2015-07-22 12:38:31
[post_modified_gmt] => 2015-07-22 02:38:31
[post_content_filtered] => 
[post_parent] => 21
[guid] => http://website.com.au/?page_id=113
[menu_order] => 0
[post_type] => page
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[studio_list] => Array
    (
        [0] => 102
    )
[email_address] => email@gmail.com
)
....

我使用了几种不同的方法来获取post id,并设法使其工作。回答下面的

我用代码玩了一段时间,并设法让它工作。下面的回答

add_filter( 'gform_pre_render', 'freetrial_studios' );
add_filter( 'gform_pre_validation', 'freetrial_studios' );
add_filter( 'gform_pre_submission_filter', 'freetrial_studios' );
add_filter( 'gform_admin_pre_render', 'freetrial_studios' );
function freetrial_studios( $form ) {
foreach ( $form['fields'] as &$field ) {
    if ( $field->type != 'select' || strpos( $field->cssClass, 'studio-list' ) === false   ) {
        continue;
    }
    //$studio_id = $GLOBALS['post']->ID; this was returning errors in the wp-admin
    $studio_id = get_the_ID();
    $studios = get_field('field_557a974776dd2', $studio_id);
        if( $studios ) {
            $choices = array();
            foreach ( $studios as $studio ) {
                // Populate the drop down field with values                         
                $choices[] = array( 'text' => $studio->post_title, 'value' => $studio->post_title );
                $field->choices = $choices;   
            }
            $field->placeholder = 'Select a Studio';
            $field->choices = $choices; 
        }       
}
return $form;
}