最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - update_post_meta() throws Uncaught error: Cannot create duplicate attribute

programmeradmin1浏览0评论

In my plugin I'm creating a post of a custom post type. Immediately after creation I'm trying to update some of the metadata that I've defined in metaboxes for this CPT. Upon execution of the code, the post gets created, but I get a fatal error.

Fatal error: Uncaught Error: Cannot create duplicate attribute

I can trace it back to the use of the update_post_meta() function. If I remove this, the code runs without errors (but doesn't do what I want).

Any ideas or suggestions?

Thanks

            //Create new post of custom post type match
            $author_id = 1;
            $match_name = $match_details['NoInTournament'] . ":" . $match_details['TeamAName'] . " vs. " . $match_details['TeamBName'];
            $slug = sanitize_title($match_name); //Will come from retrieved data
            $title = esc_html($match_details["RoundName"] . '/' . $match_name); //Will come from retrieved data
            $post_content = '<p>[rat-match-details match="' . $match . '"]</p>';

            $page = get_page_by_title( $title , OBJECT, 'match');

            if( $page == null) {
                // Create the page
                // Set the page ID so that we know the page was created successfully
                $post_id = wp_insert_post(
                    array(
                        'comment_status'    =>  'closed',
                        'ping_status'       =>  'closed',
                        'post_author'       =>  $author_id,
                        'post_name'         =>  $slug,
                        'post_title'        =>  $title,
                        'post_status'       =>  'publish',
                        'post_type'         =>  'match',
                        'post_content'      =>  $post_content
                    )
                    );
            } else {
                // The page exists
                $post_id = wp_update_post(
                    array(
                        'ID'                =>  $page->ID,
                        'post_content'      =>  $post_content
                    )
                    );
            } // end if

            //update metadata
            //Currently gives fatal error: "Cannot create duplicate attribute" ==> What is the cause?

            update_post_meta( $post_id, 'rat-match-tournament', get_the_title(get_the_ID()) );
            update_post_meta( $post_id, 'rat-match-number-vis', $match );
            update_post_meta( $post_id, 'rat-match-number-tournament', $match_details['NoInTournament'] );
            update_post_meta( $post_id, 'rat-match-round', $match_details["RoundName"] );

EDIT Here's the full error message I see on the page:

Fatal error: Uncaught Error: Cannot create duplicate attribute in /srv/www/wordpress-default/public_html/wp-includes/formatting.php:4742 Stack trace: #0 /srv/www/wordpress-default/public_html/wp-includes/formatting.php(2691): map_deep(Object(SimpleXMLElement), 'stripslashes_fr...') #1 /srv/www/wordpress-default/public_html/wp-includes/formatting.php(5342): stripslashes_deep(Object(SimpleXMLElement)) #2 /srv/www/wordpress-default/public_html/wp-includes/meta.php(182): wp_unslash(Object(SimpleXMLElement)) #3 /srv/www/wordpress-default/public_html/wp-includes/post.php(2061): update_metadata('post', 1825, 'rat-match-round', Object(SimpleXMLElement), '') #4 /srv/www/wordpress-default/public_html/wp-content/plugins/referee-assesment/includes/rat-shortcodes.php(113): update_post_meta(1825, 'rat-match-round', Object(SimpleXMLElement)) #5 /srv/www/wordpress-default/public_html/wp-includes/shortcodes.php(325): rat_shortcode_matches(Array, '<p>Hello World!...', 'rat-list-tourna...') #6 [internal function]: do_shortcode_tag(Array) #7 in /srv/www/wordpress-default/public_html/wp-includes/formatting.php on line 4742

and the one in the wordpress debug.log:

[23-Aug-2019 08:15:31 UTC] PHP Fatal error:  Uncaught Error: Cannot create duplicate attribute in /srv/www/wordpress-default/public_html/wp-includes/formatting.php:4742
Stack trace:
#0 /srv/www/wordpress-default/public_html/wp-includes/formatting.php(2691): map_deep(Object(SimpleXMLElement), 'stripslashes_fr...')
#1 /srv/www/wordpress-default/public_html/wp-includes/formatting.php(5342): stripslashes_deep(Object(SimpleXMLElement))
#2 /srv/www/wordpress-default/public_html/wp-includes/meta.php(182): wp_unslash(Object(SimpleXMLElement))
#3 /srv/www/wordpress-default/public_html/wp-includes/post.php(2061): update_metadata('post', 1825, 'rat-match-round', Object(SimpleXMLElement), '')
#4 /srv/www/wordpress-default/public_html/wp-content/plugins/referee-assesment/includes/rat-shortcodes.php(113): update_post_meta(1825, 'rat-match-round', Object(SimpleXMLElement))
#5 /srv/www/wordpress-default/public_html/wp-includes/shortcodes.php(325): rat_shortcode_matches(Array, '<p>Hello World!...', 'rat-list-tourna...')
#6 [internal function]: do_shortcode_tag(Array)
#7 in /srv/www/wordpress-default/public_html/wp-includes/formatting.php on line 4742

Note: I know it's not smart to create pages in a shortcode, but it's a first PoC for something bigger and I'll move code around to appropriate places

发布评论

评论列表(0)

  1. 暂无评论