WordPress.org

Ready to get started?Download WordPress

Forums

Setting multiple object terms (3 posts)

  1. Code Metic
    Member
    Posted 1 year ago #

    Whats wrong with this? In setting multiple object terms.

    foreach($_POST['status'] as $status) {
        wp_set_object_terms($post_id, $status, 'status', true);
    }
  2. michael.mariart
    Member
    Posted 1 year ago #

    This is pretty clear in the Codes page for wp_set_object_terms().

    Under 'Parameters', look at what it says for $terms:

    will replace all existing related terms in this taxonomy

    So, when you call that function over and over in the loop, you are over-writing all of the terms each time so you'll only ever get the last one saved.

    To get around this, get the terms into an array before you set them. But seeing as how you already have them from $_POST you don't need to worry about that.

    wp_set_object_terms($post_id, $_POST['status'], 'status');

    Of course, you also need to ensure that your stauts values are being sent as an array of terms, and not just a single term.

  3. Code Metic
    Member
    Posted 1 year ago #

    Still does not work.

    What i want that all in $_POST['status'] will be connected with term 'status'.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.