• Resolved momasvii

    (@momasvii)


    Hi,

    I am trying to find the function to add information into the table ‘wp_term_relationships’.
    I have a term in the wp_terms table with the data:
    21, New Scenario, new-scenario, 0
    So I have tried

    $cat_ids = array( 21 );
    $term_taxonomy_ids = wp_set_post_categories( $post_ID, $cat_ids, true );
    if ( is_wp_error( $term_taxonomy_ids ) ) {
    	// There was an error somewhere and the terms couldn't be set.
    	echo "1";
    } else {
    	echo "2";
    	// Success! The post's categories were set.
    }

    But that doesn’t seem to add anything. I have also tried several other functions to no avail but from what I have read I think I may be closest with this effort.

    Can someone help me add the “New Scenario” term to the Post ID 1234

    Thanks

    • This topic was modified 9 years, 7 months ago by momasvii.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @momasvii,
    I think you can do your requirement with the function:
    wp_set_object_terms( $object_id, $terms, $taxonomy );
    This function will add the post with the taxonomy term and will set its relationship also in the “terms_relationship” table.
    You can read about this function from here: wp_set_object_terms

    Hope it helps! 🙂

    Thread Starter momasvii

    (@momasvii)

    Hi,

    I have change my code to the below and even though my success message is ‘2’ i don’t see anything in the DB.

    EDIT: I have it working with this code but I do not want it to create a new term in the wp_terms table. How can I stop this from happening even though the slug already exists in there.

    Thanks

    $term_taxonomy_ids = wp_set_object_terms( $post_ID, ‘new-scenario’, ‘category’, false );
    if ( is_wp_error( $term_taxonomy_ids ) ) {
    // There was an error somewhere and the terms couldn’t be set.
    echo “1”;
    } else {
    echo “2”;
    // Success! The post’s categories were set.
    }`

    Am I using the ‘category’ variable correct because I’m not sure what that does.

    Thanks for the help.

    • This reply was modified 9 years, 7 months ago by momasvii.
    Thread Starter momasvii

    (@momasvii)

    I have fixed my issue. I needed to change category to the type in the wp_term_taxonomy table.

    Thank you for the help!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Inserting into wp_term_relationships’ is closed to new replies.