0

So I am stuck on this problem that I am facing when creating a custom taxonomy term in a install.php file.

So I am running a WP multisite, and upon new blog creations, I am trying to auto create posts and taxonomy terms. The taxonomy and CPT are already created via a plugin that is Network Activated.

Here is a snippet of what is inside the install.php file:

    // Sample Category
    $cat_name = __('Sample Category');
    /* translators: Default category slug */
    $cat_slug = sanitize_title(_x('Sample Category', 'Default category slug'));

    if ( global_terms_enabled() ) {
        $cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
        if ( $cat_id == null ) {
            $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
            $cat_id = $wpdb->insert_id;
        }
        update_option('example_product_cat', $cat_id);
    } else {
        $cat_id = 99;
    }

    // Sample Post
    $now = date('Y-m-d H:i:s');
    $now_gmt = gmdate('Y-m-d H:i:s');
    $post_guid = get_option('home') . '/sample';

    $wpdb->insert( $wpdb->posts, array(
                                'post_author' => $user_id,
                                'post_date' => $now,
                                'post_date_gmt' => $now_gmt,
                                'post_content' => '',
                                'post_excerpt' => 'Sample Text Excerpt ',
                                'post_title' => __('Sample Name'),
                                /* translators: Default post slug */
                                'post_name' => sanitize_title( _x('sample-name', 'Sample Name') ),
                                'post_modified' => $now,
                                'post_modified_gmt' => $now_gmt,
                                'guid' => $post_guid,
                                'post_type' => 'custom_post_type',
                                'comment_count' => 0,
                                'to_ping' => '',
                                'pinged' => '',
                                'post_content_filtered' => ''
                                ));
    $wpdb->insert( $wpdb->postmeta, array( 'post_id' => 4, 'meta_key' => '_sample_meta', 'meta_value' => '19' ) );
  $wpdb->insert( $wpdb->term_relationships, array( 'term_id' => 99, 'object_id' => 1) );

So the Taxonomy term is created, and so is the post, which both are viewable in the backend. The problem is, the term is not connected to the post that is created

The meta_value does not also display on the WP_List_Table, but displays on the update post page. When I click "update post", than it shows on the WP_List_table.

Is there any clue on why the taxonomy term is not added to the post, and also why the meta_value displays in the Update Post page, but not on the WP_List_Table?

Thanks! Roc.

  • So, I fixed the meta value problem in the WP_List_table by this snippet: case "service_price" : $meta = get_post_meta($post->ID, '_sample_meta', true); if ( $meta ) echo $meta; else echo '–'; break; , but still cannot figure out why the taxonomy term is not being linked to the post. Any help is appreciated. Thanks! – Roc Jun 9 '13 at 15:01
0

I believe I found the solution here: http://codex.wordpress.org/Function_Reference/wp_set_object_terms

wp_set_object_terms( $post_id, $terms, $taxonomy, $append );

wp_set_object_terms( 4, $cat_id, 'example_product_cat');

This is still placed inside of the install.php file.

If there is a better solution, please add!

Thanks. Roc.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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