1

Question

I am creating my first plugin and in it users will be able (among other things) to create new post via custom form from wordpress backend. This post uses custom post type and custom taxonomy. Form creates new post but it fails to set custom taxomony.

Creating post regularly (not via my custom form) works fine ie. allows me to add custom taxomony to it.

Custom post type and custom taxonomies are registered by the book, terms are already inserted in db.

*taxonomy is not a variable for debugging purposes

    add_action('plugins_loaded', 'newpost');

    function newpost() {
        if (isset($_POST['new_post']) == '1') {
            $post_title = $_POST['posttitle'];
            $post_content = $_POST['postcontent'];
            $new_post = array(
                'ID' => '',
                'post_author' => 1,
                'post_type' => 'cars',
                'post_content' => $post_content,
                'post_title' => $post_title,
                'comment_status' => 'closed',
                'ping_status' => 'closed',
                'post_status' => 'publish',
                'tax_input' => array('cars' => array('bmw', 'audi'))
            );

            $post_id = wp_insert_post($new_post);
        }
    }

I've even tried to remove 'tax_input' and use wp_set_post_terms and wp_set_object_terms but results were same. Two days I'm stuck on this, so any help would be greatly appreciated...

Solution

The solution was to change plugins_loaded to init with low priority.

   add_action( 'init', 'newpost', 200 );

So the issue was that user privileges have not yet been set, upon firing the plugin.

  • well I can at least tell you with certainty that the method you've shown in your code (via wp_insert_post) will definitely not work if the person submitting the form is not a logged in user with the capability to assign post terms. I use wp_set_object_terms for front-end forms. are both your taxonomy and post type named cars? – Milo May 21 '13 at 4:50
  • User has all capabilities needed, as plugin is intended to be used only from backend. Btw. I'm testing as Super Admin. Actually those are not named cars, but still they dont share the same name. I'm going to test plugin on the other server as it is last option I can think of... – deimos May 21 '13 at 5:05
0

Have you tried wp_set_object_terms? ...which needs to be placed after your call to wp_insert_post as it requires the post ID in order to attach the correct terms, to the right taxonomy with the right post.

//add_action('plugins_loaded', 'newpost'); # will not work, user not authenticated

add_action('init', 'newpost'); // will work, user authenticated
function newpost() {
    if (isset($_POST['new_post']) == '1') {
        $post_title = $_POST['posttitle'];
        $post_content = $_POST['postcontent'];
        $taxonomy = 'cars'; //change accordingly...
        $terms = $_POST['tax_terms']; //change accordingly... 
        $new_post = array(
            'ID' => '',
            'post_author' => 1,
            'post_type' => 'cars',
            'post_content' => $post_content,
            'post_title' => $post_title,
            'comment_status' => 'closed',
            'ping_status' => 'closed',
            'post_status' => 'publish',
        );

        $post_id = wp_insert_post($new_post);

        //$terms can be an array of term IDs/slugs
        //$taxonomy is your taxonomy name, e.g. cars
        wp_set_object_terms( $post_id, $terms, $taxonomy );
    }
}
  • I did try wp_set_object_terms and wp_set_post_terms also. Tried it on other server and still not working. Fresh WP install, with no plugins. Will look back into this in a few days with clear mind... – deimos May 21 '13 at 6:50
  • After further inspection plugins_loaded hook will not work for this purpose. Switch to init, or wp. Reason it won't work is because the user is not authenticated in the system at plugins_loaded which is what wp_insert_post needs, so any hook below after_setup_theme including it, works. – userabuser May 21 '13 at 7:38
  • You got me on right track, changing it just to init didn't help, but making it low priority did. Check updated question for solution. Tnx :) – deimos May 22 '13 at 3:35
  • Glad you have it working. The correct solution for this question is to not use plugins_loaded hook for this purpose as wp_insert_post does not have require permissions at this point. The priority of the init hook is subjective to your setup, for me it works at the default priority, for you, there might be some other processes hooked onto init at higher priorities getting in your way. Now that this question is solved please don't forget to mark the answer and close out the question. All the best. – userabuser May 22 '13 at 6:06

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.