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

php - Importing hard coded custom field into acf field

programmeradmin1浏览0评论

I am in the process of migrating posts from an existing wp site into a new wp site. The existing site has a custom field called subtitle. When I import the blogs into the new site, I'd like populate my simple acf subtitle text field with these hand coded custom fields.

What is the best way to do this?

Here is the code I've found in the existing sites single.php that appears to be creating the sub title field:

$mt_post_subtitle = get_post_meta($post->ID, "mt_post_subtitle", true); <?php echo esc_attr($mt_post_subtitle); ?>

Please let me know if I'm missing something and let me know what I can add to help solve this. Thanks in advance.

I am in the process of migrating posts from an existing wp site into a new wp site. The existing site has a custom field called subtitle. When I import the blogs into the new site, I'd like populate my simple acf subtitle text field with these hand coded custom fields.

What is the best way to do this?

Here is the code I've found in the existing sites single.php that appears to be creating the sub title field:

$mt_post_subtitle = get_post_meta($post->ID, "mt_post_subtitle", true); <?php echo esc_attr($mt_post_subtitle); ?>

Please let me know if I'm missing something and let me know what I can add to help solve this. Thanks in advance.

Share Improve this question asked Mar 31, 2020 at 3:04 RLMRLM 2475 silver badges19 bronze badges 3
  • 1 If I understand it properly, you could try renaming your ACF field from subtitle to mt_post_subtitle and the get_post_meta() should continue to work. – Sally CJ Commented Mar 31, 2020 at 10:03
  • 1 @Sally CJ that worked. do you want to answer the question officially to take the credit for it? thanks so much. – RLM Commented Mar 31, 2020 at 14:48
  • Glad it worked! And I've posted an answer. :) – Sally CJ Commented Mar 31, 2020 at 22:12
Add a comment  | 

1 Answer 1

Reset to default 1

First off, you should know that get_post_meta() doesn't create any custom fields (or metadata), it only retrieves the custom field's value. But the WordPress functions that do create custom fields (for posts) are add_post_meta() and update_post_meta().

So that get_post_meta($post->ID, "mt_post_subtitle", true) simply retrieves the value of the meta/field named mt_post_subtitle for the post (referenced by that $post) and as I mentioned in the comment, you could try renaming the ACF field from subtitle to mt_post_subtitle and the get_post_meta() call in your single.php (single post) template should continue to work normally.

Alternatively, you could've also used the ACF function for retrieving custom field's value, which is get_field(), but renaming the field name is an easier option in that no code changes needed. :) But for reference, with get_field() and without changing the ACF field name, you'd change that get_post_meta() to get_field( 'subtitle', $post->ID ).

发布评论

评论列表(0)

  1. 暂无评论