1

wp_insert_post() return value says:

(int|WP_Error) The post ID on success. The value 0 or WP_Error on failure.

Okay, I' have an import routine running and all but a few (about 20) of them import correctly. The ones that fail have a return value of 0 but is_wp_error() is NOT true. How do I find out what happened and why these certain articles won't import properly? At least if is_wp_error() was returned you could print out the message.

Thanks.

P.S. Here is the code that adds the article. Note: I'm also not sure why AdminNotice::displayError doesn't do anything (that's why I added log output). Am I supposed to put some type of tag on the page (it's my first ever plugin for a single task)? I got that bit of code from How to add an admin notice upon post save/update

       /**
        * Adds a knowledge base article and assign tags and category
        * @param $title The title of the article
        * @param $content The content of the article
        * @param $category The category of the article
        * @param $tags An array of tags to assign to the article
        */
        function add_ht_kb_article($title, $content, $category, $tags = array() )
        {

            $content=trim($content);
            if( !empty ( $content ) ){
                $new_article = array(
                      'post_content'   => wp_filter_post_kses($content),
                      'post_title'     => $title,
                      'post_status'    => 'publish',
                      'post_type'      => 'ht_kb'
                    );

                $new_article_id = wp_insert_post($new_article);

                if (is_wp_error($new_article_id)) {
                    $this->log("Failed (wp_error) to add article {$title}");
                    $this->log($new_article_id->get_error_message());
                    //return false;
                }
                else if( $new_article_id != 0 ){
                    //ht_kb_categories
                    $res=wp_set_object_terms( $new_article_id, intval($category), 'ht_kb_category', true );
                    if (is_wp_error($res)) {
                        $this->log("Failed to add category ID {$category}");
                        AdminNotice::displayError("Failed to add category ID {$category}");
                        return false;
                    }
                    

                    //ht_kb_tags
                    foreach ($tags as $key => $tag) {
                        $tag_slug = sanitize_title($tag);
                        $res=wp_set_object_terms( $new_article_id, $tag_slug, 'ht_kb_tag', true );
                        if (is_wp_error($res)) {
                            $this->log("Failed to add key tag {$tag_slug}");
                            AdminNotice::displayError("Failed to add key tag {$tag_slug}");
                            return false;
                        }
                    }
                }
                else {
                    $this->log("Failed (zero) to add article {$title}");
                    AdminNotice::displayError("Failed to add article {$title}");
                    //return false;
                }
            }
            return true;
        }

1 Answer 1

2

It appears that you can ask wp_insert_post() to return a WP_Error on failure by setting the 2nd parameter to true (it defaults to false).

So if you change

$new_article_id = wp_insert_post($new_article);

to

$new_article_id = wp_insert_post( $new_article, true );

in your code, you should get WP_Error objects back that you can then inspect.

1

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.