1

I have an array of posts IDs like this: $id_post_array_full=array(239,243,246,248,250,252,255,257) I would like to add tags for each posts with this function of wordpress wp_set_post_tags().

My real problem is that I have multiple arrays of tags and for each of them I have to match with the array of posts IDs

In the first red column I have the IDs and in the second red column I have the tags that I have to add for each one.

I was thinking about that: link

Thanks for your time

  • What have you tried thus far? A couple PHP loops should make quick work of the problem – bosco Aug 2 '18 at 15:15
0

I found the solution:

My array of IDs + tags is like this:

$array_tags_and_ids = [
'239' => ["car", "animal", "dog", "cat"],
'243' => ["dsa", "ewr", "bvc", "fgn"]
];

So I do:

 foreach ($array_tags_and_ids as $key => $value) {
         // re-call the value and get only the text value
         $implode_tags_names = implode( ', ', $value );

         // wordpress function
         wp_set_post_tags( $key, $implode_tags_names, true );

     }

Done

  • The implode step is unnecessary, you can pass the array directly (internally a comma-separated list is just turned into an array anyway). – Jacob Peattie Aug 2 '18 at 16:15

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.