wordpress - 如何在 Wordpress 中获取使用 ACF 创建的帖子?

标签 wordpress plugins frontend advanced-custom-fields posting

我将其添加到我的 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 );
}

现在,我认为保存成功了,我如何获取使用此表单创建的整个帖子?

我尝试了这个(在 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'); 
    }
}

Wordpress 的 ACF 插件有很好的文档,但我无法解决我的问题。 http://www.advancedcustomfields.com/resources

最佳答案

根据ACF表单字段文档https://www.advancedcustomfields.com/resources/acf_form/ “return”字段有 2 个动态占位符

"(String) The URL to be redirected to after the form is submitted. Defaults to the current URL with a GET parameter ‘?updated=true’. A special placeholder ‘%post_url%’ will be converted to post’s permalink. A special placeholder ‘%post_id%’ will be converted to post’s ID."

例如,如果添加 'return' => '?newpost=%post_id%',,您将返回原始页面,并将新帖子 ID 作为 URL 参数,您可以使用 PHP 处理该参数在您的页面模板中

关于wordpress - 如何在 Wordpress 中获取使用 ACF 创建的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963385/

相关文章:

jquery - jQuery.extend 和 jQuery.fn.extend 之间的区别?

java - JIRA 插件多次运行单行代码

Python 命令行复选框

html - 没有 div 的 Wordpress Contact Form 7 复选框的 CSS

html - 动态折叠 div 错误对齐 - Wordpress

php - 随机放置图像,不重叠

ruby-on-rails - 最佳 Ruby on Rails 前端开发攻略

php - style.css 加载但不在 wordpress 页面上呈现样式

mongodb - gorm-Grails MongoDB在执行addTo/removeFrom时未更新列表

javascript - 搜索过滤器列表,无论词序如何,搜索严格性问题