2014-09-21 109 views
2

我将此添加到我的function.php保存后:如何获得在Wordpress中使用ACF创建的帖子?

function my_pre_save_post($post_id) 
{ 
    // check if this is to be a new post 
    if($post_id != 'new') 
    { 
     return $post_id; 
    } 

    // Create a new post 
    $post = array(
     'post_status' => 'draft' , 
     'post_title' => 'A title, maybe a $_POST variable' , 
     'post_type' => 'post' , 
    ); 

    // insert the post 
    $post_id = wp_insert_post($post); 

    // update $_POST['return'] 
    $_POST['return'] = add_query_arg(array('post_id' => $post_id), $_POST['return']);  

    // return the new ID 
    return $post_id; 
} 

add_filter('acf/pre_save_post' , 'my_pre_save_post'); 

而且在page.php文件我有这样的代码:

 $postid = get_the_ID(); 
          if($postid ==50){ //50 is a Page I created for the Form 

            $options = array(
              'post_id' => 'new',//$post->ID, // post id to get field groups from and save data to 
              'field_groups' => array(46), // this will find the field groups for this post (post ID's of the acf post objects) 
              'form' => true, // set this to false to prevent the <form> tag from being created 
              'form_attributes' => array(// attributes will be added to the form element 
               'id' => 'post', 
               'class' => '', 
               'action' => '', 
               'method' => 'post', 
              ), 
              'return' => add_query_arg('updated', 'true', get_permalink()), // return url 
              'html_before_fields' => '', // html inside form before fields 
              'html_after_fields' => '', // html inside form after fields 
              'submit_value' => 'Update', // value for submit field 
              'updated_message' => 'Post updated.', // default updated message. Can be false to show no message 
             ); 
    acf_form($options); 
} 

现在,我认为节约是全成,我如何获得使用此表单创建的全部帖子?

我想这(与ID 51页),但我得到什么:

$posts = get_posts(array(
    'post_type'  => 'event', 
    'posts_per_page' => -1, 
    'meta_key'  => 'location', 
    'meta_value'  => 'melbourne' 
)); 

if($posts) 
{ 
    foreach($posts as $post) 
    { 
     the_field('titel'); 
    } 
} 

的ACF WordPress插件是非常有据可查的,但我想不出解决我的问题。 http://www.advancedcustomfields.com/resources

回答

0

文档说你已经如果你想创建一个新的职位,以您的$options阵列内post_id值设置为new_post而不是new。此外,您还必须使用new_post数组键,该数组键包含新帖子的数据作为键的值。 查看acf_form文档。这就是我的意思:

$options = array(
    'post_id' = 'new_post', 
    'new_post' = array(
     //new post data 
    ) 
); 
相关问题