0

Hi my good friends I need your help better than losing my mind, first I have to thank "Stephen Harris" for his good article ,

-second: After a lot of searching I still don't know how to inject a custom taxonomy to a custom post type Here is my code:

  $post_id = wp_insert_post(array(
    'post_author' => $user_id,
    'post_title' => $post_title,
    'post_content' => $post_content,
    'post_category' =>   array($_POST['cat']),  // Usable for custom taxonomies too
    'tax_input' => array('offer' => $terms),
    'tags_input' => array($tags),
    'post_type' => 'classified',
    'post_status' => 'publish'
        ));

update_post_meta($post_id,'$term->term_id',$taxonomy,true);

and

   <?php
    //Set up the taxonomy object and get terms  
    $taxonomy = 'offer';
    $tax = get_taxonomy($taxonomy); //This is the taxonomy object  

    $name = 'tax_input[' . $taxonomy . ']';

    $terms = get_terms($taxonomy, array('hide_empty' => 0));
    $postterms = get_the_terms($post->ID, $taxonomy);
    $current = ($postterms ? array_pop($postterms) : false);
    $current = ($current ? $current->term_id : 0);
    ?>
    <!-- Display taxonomy terms -->  
    <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">  
        <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy ?> categorychecklist form-no-clear">  
            <?php
            foreach ($terms as $term) {
                $id = $taxonomy . '-' . $term->term_id;
                echo "<li id='$id'><label class='selectit'>";
                echo "<input type='radio' id='in-$id' name='{$name}'" . checked($current, $term->term_id, false) . "value='$term->term_id' />$term->name<br />";
                echo "</label></li>";
            }
            ?>  
        </ul>  
    </div>  

What do I miss, Thanks for the help.

1 Answer 1

1

Your code doesn't show if and how you set $terms other then that its seems ok, just try to wrap it in an array and you should check if its set first, so try this:

$new_post = array(
    'post_author' => $user_id,
    'post_title' => $post_title,
    'post_content' => $post_content,
    'post_category' =>   array($_POST['cat']),
    'tags_input' => array($tags),
    'post_type' => 'classified',
    'post_status' => 'publish'
));
if (isset($_POST['tax_input']['offer'])
$new_post['tax_input'] array('offer' => (array)$_POST['tax_input']['offer']);
$post_id = wp_insert_post($new_post);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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