[resolved] wp_set_post_tags (5 posts)

  1. DarkPaladin
    Posted 6 years ago #

    i want to make a plugin that adds a tag to a post automatically

    my code (example):

    add_action('save_post', 'agregar_tags');
    function agregar_tags($id){
    $tags=array('A tag','Another tag');

    it only creates the tags, but they aren't assigned to the post, the post says 'No tags'.

    What can i do to create the tags and asign them to the post, i thought that wp_set_post_tags() can do that

    thank you

    p.d: also tried wp_set_object_terms() and nothing

  2. DarkPaladin
    Posted 6 years ago #


  3. DarkPaladin
    Posted 6 years ago #

    please help me

    i'm using:

    $wpdb->insert( $wpdb->term_relationships,
    array( 'object_id' => $object_id, 'term_taxonomy_id' => $id ) );

    and also:

    $wpdb->query("INSERT INTO ".$wpdb->term_relationships."
    (object_id, term_taxonomy_id) VALUES ($object_id, $id)");

    but it doesn't work, it adds nothing to the db

    what can i do?

  4. josh the jenius
    Posted 6 years ago #

    I had a similar problem:

    wp_set_post_categories($wpdb->insert_id, array($req->wpcat));
    wp_set_post_tags($wpdb->insert_id, $tags);

    wp_set_post_categories uses the $wpdb object to insert, thus changing the insert ID.

    I fixed it by doing this:

    $post_ID = $wpdb->insert_id;
    wp_set_post_categories($post_ID, array($req->wpcat));
    wp_set_post_tags($post_ID, $tags);

    This works as of 2.7.0.

    Hope someone finds this helpful.

    Reminder: $tags holds a single dimensional array. Ex:

    $tags = array('one', 'two', 'three');
  5. tommix
    Posted 6 years ago #

    I'm using this and it works with WP 2.8.4

    the $id is Post id.

    function write_tags($id, $tags) {
    $taxonomy = 'post_tag';
    //$tags = split(" ",$months);
    $tags = explode(' ', $tags);

    foreach ($tags as $solotag) {
    $solotag = trim($solotag);
    $check = is_term($solotag, $taxonomy);
    if (is_null($check)) {
    $tag = wp_insert_term($solotag, $taxonomy);

    if(!is_wp_error($tag)) {
    $tagid = $tag['term_id'];
    } else {
    $tagid = $check['term_id'];

    // wp_set_object_terms($id, $tagid, $taxonomy, true);



    hope this will help someone cause i need it wery much and finally i did it :)

Topic Closed

This topic has been closed to new replies.

About this Topic