Как я могу получить сообщения, созданные с помощью ACF в Wordpress?

Я добавил это в свой function.php для сохранения сообщения:

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 );
}

Теперь я думаю, что сохранение было успешным, как мне получить все сообщения, созданные с помощью этой формы?

Я пробовал это (на странице с идентификатором 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


person Suisse    schedule 21.09.2014    source источник


Ответы (1)


В документации говорится, что вы должны установить значение post_id внутри вашего массива $options на new_post вместо new, если вы хотите создать новый пост. Также вы должны использовать ключ массива new_post с массивом данных для нового сообщения в качестве значения для ключа. Ознакомьтесь с документацией acf_form. Вот что я в основном имею в виду:

$options = array(
   'post_id' = 'new_post',
   'new_post' = array(
      //new post data
   )
);
person luukvhoudt    schedule 03.11.2014