Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to insert a post via wp_insert_post and then add relevant post_meta - however, the WP admin has been customized with the excellent ACF.

I know I can add_post_meta in this way:

add_post_meta( $post_id, 'q_key_type', $q_key_type );

However, to make ACF happy, it's required to also use the "update_field" function - like so:

update_field( "field_52c737a413e07", $q_key_type, $post_id );

This method works fine, but I can't get a Taxonomy object to register correctly - I can get the value to save using add_post_meta - when I view the new post it does not recognise the Taxonomy - the value is empty, but in the postmeta table the value is stored correctly - what is missing is the correct field recognition.

I can save fields of the type "Text" and "User" correctly using these two methods.

Here is an image of two rows in the post_meta table - the first two are created by acf - the last two manually - they appear to be formatted identically:

enter image description here

I can only find one other reference to the same field name in the post_meta table - it contains an array with the following data - which seems to be the field set-up for acf:

Array (
    [key] => field_52c737a413e07
    [label] => Type
    [name] => q_key_type
    [type] => taxonomy
    [instructions] => 
    [required] => 0
    [taxonomy] => q_key_type
    [field_type] => select
    [allow_null] => 0
    [load_save_terms] => 1
    [return_format] => object
    [conditional_logic] => Array (
        [status] => 0
        [rules] => Array (
            [0] => Array (
                [field] => null
                [operator] => ==
                [value] => 
            )
        )
        [allorany] => all
    )
[order_no] => 3
)

So, the question is - where else is ACF saving the taxonomy data - I can't find anything in the terms tables?

share|improve this question
    
Should I be doing this with codex.wordpress.org/Function_Reference/wp_set_object_terms ? –  Q Studio Jan 5 at 21:09

1 Answer 1

up vote 0 down vote accepted

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 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.