0

I have custom template and i need get og:image url in header. Class image location in DB wp_postmeta - meta_value i try with functions.php

function getOgImage()
{
    global $wpdb;
    $ogimage = $wpdb->get_results("SELECT DISTINCT post_id FROM wp_postmeta WHERE meta_value", OBJECT);

    echo '<meta property="og:image" content="'.$ogimage.'"/>';
}
add_action('wp_head', 'getOgImage');

Im geting out:

<meta property="og:image" content="Array" />
0

Use var_dump($ogimage); to understand the contents of $ogimage presumably it's returning an array of records from the database.

Generally you would retrieve post meta values with get_post_meta

WordPress Docs for get_post_meta()

Also there are plugins that sort out the og tags for you such as Yoast SEO

Yoast SEO plugin page on WordPress site

| improve this answer | |
0

Problem solved!

Code:

function getOgImage()
{
    global $wpdb;
    $ogimage = get_post_meta(get_the_ID(), '_wcs_image', true);

    echo '<meta property="og:image" content="'.$ogimage.'"/>';
}
add_action('wp_head', 'getOgImage');

Thanks to Bob!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.