admin管理员组

文章数量:1122832

I have very confused situation. I have added posts and all posts metas from one website db to another one, everything is fine, images, title, description of posts are there.

But post metas are not showing on webpage(img1), although they are exists when I go to post edit page in wp-admin(img2), I can see the post data , but they are not showing until I click to 'Update' button myself.

After that all post data is showing.(img3,img4)

Posts are to much (about 4000 recipes) , so I can't do it manually, it will take a long time.

So I need something to trigger every post updating automatically.

I have tried to update post meta, but it doesn't work, it still have to update post manually.

$metas = get_post_meta(get_the_ID());
foreach($metas as $meta_key => $meta_value){
  foreach( $meta_value as $val ){
    update_post_meta( get_the_ID(), $meta_key, $val);
  }
}  

Is there a way to do that?

IMG1

IMG2

IMG3

IMG4

I have very confused situation. I have added posts and all posts metas from one website db to another one, everything is fine, images, title, description of posts are there.

But post metas are not showing on webpage(img1), although they are exists when I go to post edit page in wp-admin(img2), I can see the post data , but they are not showing until I click to 'Update' button myself.

After that all post data is showing.(img3,img4)

Posts are to much (about 4000 recipes) , so I can't do it manually, it will take a long time.

So I need something to trigger every post updating automatically.

I have tried to update post meta, but it doesn't work, it still have to update post manually.

$metas = get_post_meta(get_the_ID());
foreach($metas as $meta_key => $meta_value){
  foreach( $meta_value as $val ){
    update_post_meta( get_the_ID(), $meta_key, $val);
  }
}  

Is there a way to do that?

IMG1

IMG2

IMG3

IMG4

Share Improve this question edited Oct 14, 2017 at 10:19 Anna Gabrielyan asked Oct 14, 2017 at 8:00 Anna GabrielyanAnna Gabrielyan 13311 bronze badges 8
  • I don't understand "I can see the post data , but they are not showing". where are they not showing ? in the admin edit page ? – mmm Commented Oct 14, 2017 at 8:17
  • I can see the post data in admin page, but it doesn't show in webpage, untillI click the update button from admin – Anna Gabrielyan Commented Oct 14, 2017 at 8:24
  • Are you using advanced custom fields or something similar? Do you use get_field to retrieve the field values or get_post_meta? – janh Commented Oct 14, 2017 at 8:50
  • Are you using some sort of caching plugin? – Jacob Peattie Commented Oct 14, 2017 at 8:59
  • 1 Mh. ACF's get_field() doesn't work unless you've added the field references as well (e.g. myfield = myvalue, _myfield = field_26ae812...), but that shouldn't matter if you're using get_post_meta to retrieve the data in the frontend. – janh Commented Oct 14, 2017 at 10:16
 |  Show 3 more comments

1 Answer 1

Reset to default 0

I've used this to insert posts with ACF/postmeta data successfully, but please test it on a development site before deploying live, there might be some terrible assumptions. Generally, it assumes that you only have each name once, that is "myfield" doesn't exist in two field groups with different meanings.

$mycustomvalue = 123;
$f_mycustomfield = get_acf_key_by_name("mycustomfield");
update_post_meta( $postid, "mycustomfield", $mycustomvalue );
update_post_meta( $postid, "_mycustomfield", $f_mycustomfield["key"] );


function load_acf_names2key() {
    global $acf_names2key;
    $acf_names2key = array();
    $posts = get_posts(array(
        'numberposts'   => -1,
        'post_type'     => 'acf',
        'orderby'       => 'menu_order title',
        'order'         => 'asc',
        'suppress_filters' => false,
    ));
    foreach($posts as $fieldgroup) {
        $fields = get_post_meta( $fieldgroup->ID );
        foreach($fields as $fieldkey => $fieldmeta) {
            if(substr($fieldkey, 0, 6) == "field_") {
                $field = get_post_meta( $fieldgroup->ID, $fieldkey);
                foreach($field as $subfield) {
                    if(!array_key_exists($subfield["name"], $acf_names2key)) $acf_names2key[ $subfield["name"] ] = array();
                    $subfield[ "group" ] = $fieldgroup->post_name;
                    array_push( $acf_names2key[ $subfield["name"] ], $subfield );
                }
            }
        }
    }
}

function get_acf_key_by_name($name, $options = array()) {
    global $acf_names2key;
    if(!$acf_names2key || !is_array($acf_names2key) || count($acf_names2key) == 0) {
        load_acf_names2key();
    }
    if(!array_key_exists($name, $acf_names2key))
        return false;
    foreach($acf_names2key[ $name ] as $possible) {
        foreach($options as $key => $val) {
            if(preg_match("!^/.*/i?$!", $val)) {
                if(!preg_match($val, $possible[ $key ])) {
                    continue 2;
                }
            }
            else if($possible[ $key ] != $val) {
                continue 2;
            }
        }
        # no options? this will return the first one
        # has options? will return the first item that
        #     satisfies all options
        return $possible;
    }
    return false;
}

It's been quite a while since I wrote this and memory fails me what exactly I wanted to do with those $options that can be passed to get_acf_key_by_name (or if I just copy pasted that code from somewhere), but it always worked for me. I haven't tried to work with repeater fields, so that might be a challenge, but give it a try.

本文标签: How trigger to save post when updating post meta