I want to overwrite the post tag with the value of the custom field.

Asked 1 years ago, Updated 1 years ago, 75 views

You would like to change the post tag to the value of one or more specific custom fields.
Only one custom field could be reflected as a tag, but multiple custom field values could not be reflected as a tag.

Here's the code I've tried so far

<?php
global$post;
$post_id = $post->ID; 
$tags[] = get_post_meta($post_id, 'aaaa', $single);
$tags[] = get_post_meta($post_id, 'bbbb', $single);
$tags[] = get_post_meta($post_id, 'cccccc', $single);
$tags=array_unique(array_filter($tags);//here, adding array_filter to remove empty get_post_meta results
wp_set_post_tags($post_id, $tags, false);
?>

For the above code, only the custom key listed at the top was reflected in the tag.

global$post;
$post_id = $post->ID; 
$tags[] = get_post_meta($post_id, 'aaaa', $single);
$tags[] = get_post_meta($post_id, 'bbbb', $single);
$tags[] = get_post_meta($post_id, 'cccccc', $single);
$tags=array_unique(array_filter($tags);//here, adding array_filter to remove empty get_post_meta results
foreach($tags as$tag){
    wp_set_post_tags($post_id,$tag,false);
}

Only the custom key at the top of this code was reflected as a tag.

<?php
global$post;
$post_id = $post->ID;

$tags=get_post_meta($post_id, 'aaaaa', $single);
if(!empty($tag){wp_add_post_tags($post_id,$$tag);}

$tags=get_post_meta($post_id, 'bbbb', $single);
if(!empty($tag){wp_add_post_tags($post_id,$$tag);}

$tags=get_post_meta($post_id, 'cccccc', $single);
if(!empty($tag){wp_add_post_tags($post_id,$$tag);}

wp_set_post_tags($post_id, $tags, false);   

?>

None of the values in the custom fields were reflected as tags in the code above.

wordpress

2022-09-30 16:40

1 Answers

It seems that the reason was that it didn't come back with a string, and the third argument, true, worked easily!


2022-09-30 16:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.