0

I have a custom post type books and 3 custom taxonomies attached to books. The custom taxonomies are: books-categories , location , services_c. How should i edit my form to get my taxonomies work; I'm trying to solve this for 3 days and but i can't. Can anyone help me please?

<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please enter a  title';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please enter the content';
    }
    $tags = $_POST['post_tags'];

    // Add the content of the form to $post as an array
    $new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'tags_input'    => array($tags),
        'post_status'   => 'draft',           // Choose: publish, preview, future, draft, etc.
        'post_type' => 'books'  //'post',page' or use a custom post type if you want to
    );
    //save the new post
    $pid = wp_insert_post($new_post); 

}

?>  

<!-- New Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">

<!-- post name -->
<p><label for="title">Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>

<!-- post Category -->
<p><label for="Category">Category:</label><br />
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=books-categories' ); ?></p>

<!-- post Location -->
<p><label for="Location">Location:</label><br />
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=location' ); ?></p>

<!-- post Services -->
<p><label for="Services">Services:</label><br />
<p><?php wp_dropdown_categories( 'show_option_none=Category&tab_index=4&taxonomy=services_c' ); ?></p>

<!-- post Content -->
<p><label for="description">Content</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>

<!-- post tags -->
<p><label for="post_tags">Tags:</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
  • This is a duplicate, of your own question - wordpress.stackexchange.com/questions/247409/… – Dan. Nov 27 '16 at 14:01
  • You need to read my answer again, and try what I posted, as I can see from the code in your new question that you missed a couple bits. when calling wp_dropdown_categories you need to add a parameter which will give the form element a different name (at the moment all those dropdowns have the same name). – Dan. Nov 27 '16 at 14:03
1

Your logic is a little flawed - you run isset checks on fields like title and description, but don't actually halt the post insertion if they're not set.

You also don't check the post_tags index, which will never exist because wp_dropdown_categories will use the field name cat unless you set it to something else.

Not to mention that tags_input is specifically for the taxonomy post_tag, so even if everything else was working, the terms would never be properly assigned.

Let's refactor and use the PHP filter API while we're at it:

<?php

if ( 'POST' === $_SERVER['REQUEST_METHOD'] && filter_input( INPUT_POST, 'action' ) === 'new_post' ) {
    $errors      = [];
    $title       = filter_input( INPUT_POST, 'title',       FILTER_SANITIZE_STRING );
    $description = filter_input( INPUT_POST, 'description', FILTER_SANITIZE_STRING );

    if ( ! $title ) {
        $errors[] = 'Please enter a title';
    }

    if ( ! $description ) {
        $errors[] = 'Please enter a description';
    }

    if ( ! $errors ) {
        $new_post_id = wp_insert_post([
            'post_title'   => $title,
            'post_content' => $description,
            'post_status'  => 'draft',
            'post_type'    => 'books',
        ]);

        if ( ! $new_post_id ) {
            $errors[] = 'Oops, something went wrong';
        } else {
            /**
             * Loop over taxonomies and attempt to set post terms.
             */
            foreach ([
                'books-categories',
                'location',
                'services_c',
            ] as $post_taxonomy ) {

                // Field name is the format "{taxonomy}_term_id"
                if ( $term_id = ( int ) filter_input( INPUT_POST, "{$post_taxonomy}_term_id" ) ) {
                    wp_set_object_terms( $new_post_id, $term_id, $post_taxonomy );
                }
            }
        }
    }

    foreach ( $errors as $error ) {
        echo "$error<br />";
    }
}

Now we just need to set the right input name for each taxonomy dropdown - I've used the format {taxonomy}_term_id so as not to confuse WordPress (if you used just the taxonomy name as the input, it will likely clash with the query_var for said taxonomy).

wp_dropdown_categories( 'name=books-categories_term_id&show_option_none=Category&tab_index=4&taxonomy=books-categories' );

Do the same for the others and you should be good to go.

| improve this answer | |
1
  add_filter('single_template', 'my_single_template');
    function my_single_template() {
        global $post;
        $this_plugin_dir = WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__));
        //echo $post->post_type; exit;
        /* Checks for single template by post type */
        if ($post->post_type == 'articles') {
            if (file_exists($this_plugin_dir . '/templates/single-articles.php')) {
                return $this_plugin_dir . '/templates/single-articles.php';
            }
        }
        return $single;
    }
| 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.