admin管理员组文章数量:1392007
I am using get_post() to call a single Wordpress post using it's post ID. I have successfully managed to pull the content / title of the post but would like to also pull custom fields also.
The code below is how I declare custom fields in a standard wp_query:
$customField = (get_post_meta($post->ID, "_mcf_customField", true));
And my get_post code below:
$my_id = 401491;
$post_id = get_post($my_id);
$customField = get_post_meta($post_id, "_mcf_customField", true); // I do not think this is correct
$content = $post_id ->post_content;
echo $content;
echo $customField; // No output
I believe the customField variable above is declared incorrectly, cannot seem to find anything in the Codex that sheds any light though. Does anyone have experience using Custom fields with get_post?
I am using get_post() to call a single Wordpress post using it's post ID. I have successfully managed to pull the content / title of the post but would like to also pull custom fields also.
The code below is how I declare custom fields in a standard wp_query:
$customField = (get_post_meta($post->ID, "_mcf_customField", true));
And my get_post code below:
$my_id = 401491;
$post_id = get_post($my_id);
$customField = get_post_meta($post_id, "_mcf_customField", true); // I do not think this is correct
$content = $post_id ->post_content;
echo $content;
echo $customField; // No output
I believe the customField variable above is declared incorrectly, cannot seem to find anything in the Codex that sheds any light though. Does anyone have experience using Custom fields with get_post?
Share Improve this question asked Oct 28, 2015 at 12:00 JamesJames 2483 silver badges14 bronze badges 1- I think, get_post_meta should be called with $my_id instead of $post_id. – M-R Commented Oct 28, 2015 at 12:03
2 Answers
Reset to default 2You already know the ID, so just use it:
$customField = get_post_meta($my_id, "_mcf_customField", true);
But only for reference, if you want to get the ID from the object:
$customField = get_post_meta($post_id->ID, "_mcf_customField", true);
With ACF, i am able to do this:
$my_field = get_field('my_field', $post->ID);
Reference: https://www.advancedcustomfields/resources/get_field/
本文标签: get postCustom Fields with getpost()
版权声明:本文标题:get post - Custom Fields with get_post() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744773120a2624463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论