Support

Account

Home Forums General Issues update_field with post_object

Solving

update_field with post_object

  • I have several posts to insert a custom field, use the ‘post object’ in the case, as I use the update field for post object?
    What I am trying to use:

    $supplier_one = 'my supplier';
    $supplier_two = 'my supplier TWO';
    $post_id_one = 555;
    $post_id_two = 556;
    
    update_field('suppliers', $supplier_one, $post_id_one)
    update_field('suppliers', $supplier_two, $post_id_two)
  • Hi @Rank

    To save the value of a post_object field, you should save the ID of the post.
    If the field has been set as a multiple value field, you need to save an array of post ID’s

    Does this help?

  • It does, although, It would be awesome to be able to remove or add individual elements, and not have to replace the entire value.

  • I do need to add both post_meta:

    add_post_meta( $insert_q_key_id, ‘q_key_type’, (int)$q_key_type ); // Q Key Type
    And for ACF:

    update_field( “field_52c737a413e07”, (int)$q_key_type, $insert_q_key_id ); // Q Key Type
    And the rest of the answer “should” be covered by wp_set_object_terms:

    wp_set_object_terms( $insert_q_key_id, (int)$q_key_type, ‘q_key_type’, true );
    However, this function is not fully available at the point I need it – so the answer was to create a simple replacement for this function:

    /**
    * simple wp_set_object_terms replacement as not available during API call
    *
    * @since 0.4
    * @global Object $wpdb
    * @param integer $object_id
    * @param integer $tt_id
    * @return Mixed Integer of inserted row on success | Boolean false
    */
    public function q_wp_set_object_terms( $object_id = null, $tt_id = null )
    {

    if ( ! $object_id || ! $tt_id ) { return false; }

    global $wpdb;

    if ( $wpdb->insert( $wpdb->term_relationships, array( ‘object_id’ => (int)$object_id, ‘term_taxonomy_id’ => (int)$tt_id ) ) ) {

    return $wpdb->insert_id;

    }

    // if not ##
    return false;

    }
    Which I can call using a static class instance ( as the method is part of a separate class in my case… ):

    Q_Key::q_wp_set_object_terms ( $insert_q_key_id, (int)$q_key_type );

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘update_field with post_object’ is closed to new replies.