dpqaaczn141761 2018-10-19 01:06
浏览 26

添加Post Meta从Array()添加2个条目而不是1个

I am trying to collect the values of all rows from a repeater field for use in a query. I know I need to collect the values and combine them in my own meta_key as ACF saves them in a way that makes it impossible otherwise.

Currently I cannot get it to save the post_meta.

Update

I have modified the code in a way I think should be in the right direction, however now I get 2 items added to the postmeta table instead of 1 item with the values combined inside it.

Functions File

add_filter('acf/save_post', 'update_display_pages', 20);
function update_display_pages($post_id) {
    if ( get_post_type($post_id) != 'popup' )
        return;

    if( have_rows('popup_pages', $post_id) ):
        $displayPages = array();

        while ( have_rows('popup_pages', $post_id) ) : the_row();
             if (get_sub_field('popup_pages_select')) {

                $pages = array();
                $pages[] = get_sub_field('popup_pages_select');

                print_r($pages);
                print_r($displayPages);

                foreach ($pages as $page) {
                    $displayPages = $pages;
                    $displayPages[] = $displayPages;
                }

                if ( get_post_meta( $post_id, 'popup_display_pages', true ) )
                    update_post_meta($post_id, 'popup_display_pages', $displayPages);
                else
                    add_post_meta( $post_id, 'popup_display_pages', $displayPages);
             }
      endwhile;
    endif;
}

Saves As

Meta Key: popup_display_pages
Meta Value: a:2:{i:0;s:33:"https://www.yourdomain.co.uk/page";i:1;a:1:{i:0;s:33:"https://www.yourdomain.co.uk/page";}}

Meta Key: popup_display_pages
Meta Value: a:2:{i:0;s:34:"https://www.yourdomain.co.uk/pager";i:1;a:1:{i:0;s:34:"https://www.yourdomain.co.uk/pager";}}

I want it to save it in 1 Meta Key and store both values however as you can see it just duplicates the first value and then creates another entry and does the same for the other value.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害