• Resolved darkpaladin

    (@darkpaladin)


    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');
    wp_set_post_tags($id,$tags);
    }

    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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter darkpaladin

    (@darkpaladin)

    =/

    Thread Starter darkpaladin

    (@darkpaladin)

    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?

    josh-the-jenius

    (@josh-the-jenius)

    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');

    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);

    }

    }
    wp_set_post_tags($id,$tags);
    }

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_set_post_tags’ is closed to new replies.