3

I'm having issues creating an import / caching script to be used within a WordPress site. This feed comes from a single text file that is then parsed and returned as an array.

When running through each item from the feed I am using the wp_insert_post function to add in the data (see below):

$post = array(
    'post_title'        => $property['DISPLAY_ADDRESS'],
    'post_name'         => sanitize_title($property['DISPLAY_ADDRESS'] . "-" . $property['AGENT_REF']),
    'post_content'      => $content,
    'post_status'       => 'publish',
    'post_type'         => 'property',
    'page_template'     => 'page-property.php'  
);  

$post_id = wp_insert_post( $post, true );

When I output the $post_id to catch the error I get the below:

WP_Error Object
(
    [errors] => Array
        (
            [db_update_error] => Array
                (
                    [0] => Could not update post in the database
                )

        )

    [error_data] => Array
        (
        )

)

I have tried taking the actual text that is in the $content varilable and adding that straight into the 'post_content' option and that works fine. It is somewhere between my text and it being passed as $content that is causing issues. I have also tried using the 'post_content_filtered' option, which didn't make a difference.

Has anyone had this issue before or know why this error will be triggered? Without the 'post_content' field it works fine.



*** UPDATED ***

The $content variable contains text from the feed. Shown below with some data blanked out as its for a client. Below is an example of $post outputted to show the data.

Array
(
    [post_title] => Example Post Title
    [post_name] => example-post-name
    [post_content] =>  ***£303.33 pcm***    Available 1st July 

Rent includes all utility bills. A self contained room with your own kitchenette and shared bathroom with one other .
A popular location close shops and all main roads . 
    [post_status] => publish
    [post_type] => property
    [page_template] => page-property.php
    [post_content_filtered] => 
)
  • Is $content empty? – s_ha_dum Jun 25 '15 at 18:41
  • I have just updated the question above with an example of what the $content shows that is being passed into the 'post_content' field. – Jamie Jun 25 '15 at 18:52
  • you should only get the Could not update post in the database error if you're passing ID to update an existing post. Something doesn't seem right here if you're not setting that and getting this error. – Milo Jun 25 '15 at 19:19
  • 2
    This has to be an encoding issue. What language are you using and what is the collation of the database table? – s_ha_dum Jun 25 '15 at 19:57
  • 1
    Brilliant suggestion @s_ha_dum! I have just wrapped the $content in utf8_encode and that is now working. Will need a few tweaks for the pound symbols but there is no query error now. – Jamie Jun 25 '15 at 20:14
6

Today, I was trying to insert an imported data into WordPress. I was using wp_insert_post to insert data, and it threw the following error:

WP_Error Object (
    [errors] => Array
    (
        [db_update_error] => Array
            (
                [0] => Could not update post in the database
            )

    )
    [error_data] => Array
    (
    )
)

My friend said me that I was trying to insert an non-utf8 encoded data, and WordPress uses UTF8 internally.

$post_info['post_content'] = iconv('ISO-8859-1','UTF-8', $post_info['post_content']);
  • Another solution: $post_info['post_content'] = utf8_encode( $post_info['post_content'] ); – Rufein Jan 22 '17 at 11:19

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.