0

I have a custom post type that I am setting up so that different sets of custom fields are displayed based on the type of post it is. It's one of three (collection, product, or variant).

I have everything set up in that regard, but I am thinking that when designing my templates to display different data depending on the input type, it may be easier to do it if I had the input type defined as a taxonomy.

So I would like to update my post with the corresponding taxonomy that relates to the input type.

I came up with this, but it is not doing the trick.

To clarify, my taxonomy:

function set_tax_terms() {
    global $wpdb;
    $post_id = $wpdb->get_results ('SELECT ID FROM wp_hyhg_posts WHERE post_type = uc-collections' , object);

    foreach ($post_id as $id) {
        //get the input_type meta 
        $term= get_post_meta( $id, 'input_type', true );
        //get the term id that matches the input type
        $term_id = term_exists( $term, 'input' );
        //set the post term based on above results
        wp_set_post_terms( $id, $term_id, 'input', true ); 
    }
}

set_tax_terms();

Any ideas?

Based on another post, I also tried this method with no success. (I am putting this code in a snippet plugin, so I can easily activate it and deactivate.)

function set_tax_terms() {              
    $args = array('post_type' =>'uc-collections');                
    $post_id=get_post( $args );

    if($post_id) {
        foreach ($post_id as $id) {
            //get the input_type meta 
            $term= get_post_meta( $id->ID, 'input_type', true );
            //get the term id that matches the input type
            if(!empty($term)) {
                $term_id = term_exists( $term, 'input' );
                //set the post term based on above results
                wp_set_post_terms( $id->ID, $term_id, 'input', true ); 
            }
        }
    }
}

set_tax_terms();

For some reason I am not getting the code set up below to work. So I'm trying to figure out how things are related in the database.

In the term table: my term_id/ slug relationship for the items in question are : 57/ collection, 58/product, 59/ variant.

This is what I would assume would be in the term_relationships table, but that doesn't seem to be the case.

I see object_id and term_taxonomy_id, one example being 3838 / 67 which corresponds to a post That I defined a term for, and the term_taxonomy table. I am seeing that there is a term_taxonomy_id, and then term_id as 67 / 59 which would be my variant selection.

I guess I'm not sure why there are two different numbers in this case. I don't know why this doesn't want to work. Guess I'm going do it manually.

  • Are you trying to set the terms from meta on post save? What the application for setting your terms? – DrewAPicture Sep 8 '14 at 3:58
  • well I already have all the post defined, so I dont want to go through 700+ entries to define the taxonomy, I just need to update it with a query. I don't understand your second question. – Jon Cody Sep 8 '14 at 4:18
  • What form are you storing the term in meta as? Is it the term_id or slug? Because if it's one of those two, you can avoid the term_exists() call which hits the database each time. Working on an example for you. – DrewAPicture Sep 8 '14 at 4:38
0

You should be able to just the following:

Just a couple of things to note:

  • By using get_term_by() instead of term_exists(), you hit the term cache instead of the database for every retrieval of the term object.
/**
 * Set terms for the given post type based on a meta value.
 */
function wpdocs_set_terms_for_post_types() {
    $posts = get_posts( array(
        'posts_per_page' => -1,
        'post_type'      => 'uc-collections',
    ) );

    if ( ! empty( $posts ) ) {
        foreach ( $posts as $post ) {
            if ( $meta = get_post_meta( $post->ID, 'input_type', true ) {
                if ( is_string( $meta ) && $term = get_term_by( 'slug', strtolower( $meta ) ) ) {
                    wp_set_post_terms( $post->ID, $term->slug, 'input' );
                }
            }
        }
    }
}
wpdocs_set_terms_for_post_types();
  • well im still not 100% sure if I'm understanding the term id in meta. I set the term id because the codex says that if you have heretical taxonomies, you need a term id. one row of data would look like this meta_id, post_id= 3311, meta_key= input_type, meta_value= variant. Im not sure if it makes a difference if my taxonomies are heiretical or not. but I did set them as such, hence the code above. collection> product> variant – Jon Cody Sep 8 '14 at 5:04
  • I need you to be more specific. You're storing a value in a meta key called 'input_type'. What are you storing there? Is it a term slug, is it a term ID. What is it? – DrewAPicture Sep 8 '14 at 5:07
  • and sorry, i submitted the comment too early – Jon Cody Sep 8 '14 at 5:08
  • and I only have one post type I am editing. uc-collections – Jon Cody Sep 8 '14 at 5:10
  • The problem is that in order to set a term using a meta value, you need to store something in that meta value that directly correlates to an existing term. So if the term slug is 'collections' and you store it in meta, you can retrieve it, use that value to retrieve the term object and assign that object to the post. So in order to help you, I need to know what value(s) you're storing using the meta key 'input_type'. – DrewAPicture Sep 8 '14 at 5:11

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.