Home > Software engineering >  I can't update post meta for WordPress
I can't update post meta for WordPress

Time:08-27

I'm trying to define the image I took from a remote site as a featured image, but I can't do it. I tried 2 different methods below but neither of them worked. How can I do it, can you help me?

Method 1 - doesn't work:

$postmeta->meta_value = $postmeta->meta_value.$imagelink;
                  update_post_meta($postmeta);

Method 2 - doesn't work:

update_post_meta(['post_id' => $post_id, 'meta_value' => $imagelink]);

I don't know if it's done differently. My goal is to add the image from the other site to the topic on my own site as a featured image.

I would like to add. there are topics on my site. The topics do not have featured images. So I guess I need to update and add it.

CodePudding user response:

The first parameter to update_post_meta() is the post id. The second is the meta key, and the third is the meta value. The optional fourth parameter is a previous value to check.

So you'll need something like this:

update_post_meta( $post_id, 'aross_imagelink', $imagelink, $imagelink );

Every entry in a WordPress meta table needs a meta_key. And, all plugins and other code can come up with their own keys, so it's necessary to make sure your key doesn't collide with somebody else's. That's why I suggest you use something that's probably unique like 'aross_imagelink'.

This answer doesn't cover the question of how you make an image the featured image, just how to update post meta values.

CodePudding user response:

set_post_thumbnail() can be used to set a feature image for a post. You can find the full code under User Contributed Notes.

  • Related