Forums

Programmatically setting featured image (3 posts)

  1. Warren Harrison
    Member
    Posted 1 year ago #

    I know I can get a post's featured image using get_the_post_thumbnail() but how would I go about programmatically setting a post's featured image in a plugin. After looking through the data in the wp_posts and wp_postmeta tables, I can see no difference between an image embedded within a post and one set as a featured image.

    Any help is greatly appreciated.

  2. Inigo Gonzalez
    Member
    Posted 1 year ago #

    The only way I found is to insert a post meta with the key _thumbnail_id.

    for example: add_post_meta($post_id, '_thumbnail_id', $attachment_id);

    Hope this helps

  3. Warren Harrison
    Member
    Posted 1 year ago #

    aha! I will give that a whirl. Thanks so much for the info.

Topic Closed

This topic has been closed to new replies.

About this Topic