I didn’t find similar questions like mine, so I hope someone can explain to me what I am doing wrong.
I would like to echo from the backend wordpress custom fields via a function. This is what I do:
Function
function add_meta_data() {
if ( is_page() ) {
echo'<meta property="og:title" content="' . get_post_meta( $post_id, 'og_title', true ) . '">';
}
add_action('wp_head','add_meta_data',1);
The above returns in an empty meta data:
Before you comment about “Why not using existing SEO plugins?”, that is the whole thing I’d like to eliminate plugins which come with too many unused function and try to use existing features from WP.
3
Answers
The mistake I made in the function is that
$post_id
is not defined, as this is not a global variable. What needs to be done is:use this function with post id/
Thanks
If you are saving the og_title through get_post_meta in db then you will definitely have the og_title. But seems you are not passing the post_id which is mandatory.
Please go through the doc mentioned on WP
You need to put the post_id to get the meta data. See the code below