最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom field - Why isn't update_post_meta saving?

programmeradmin1浏览0评论

I am trying to concatenate 2 custom meta fields into one, and saving it to a new custom meta field. The data of those fields is created using a frontend submit plugin that uses the default WordPress functions to save posts. (The data of those fields does save correctly)

The problem is that on Publish, the concatenated field s not saving. However, the meta key is created.

What am I doing wrong? The only thing I can think of is that this code if firing before the other two fields are saved. Is there any way for me to check that?

Here is my code:

add_action('publish_post', 'concat_data');
function concat_data(){
    if ( !empty( get_post_meta( get_the_ID(), 'offer_one', true ) ) ) {

        $offer_two = get_post_meta( get_the_ID(), 'offer_two', true );
        $offer_one = get_post_meta( get_the_ID(), 'offer_one', true );

        if(!empty($offer_two) && !empty($offer_one)) {
        $concat_data = "Kitchen ".$offer_one." Reg: ".$offer_two;
        }

        else if(empty($offer_two) && !empty($offer_one)) {
        $concat_data = "Kitchen ".$offer_one;
        }

        update_post_meta( get_the_ID(), 'concat_data', $concat_data );
    }
}
发布评论

评论列表(0)

  1. 暂无评论