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