Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

update_post_meta / add_post_meta fails WordPress

  • SOLVED

Hi guys,

My client has a WP site, they have the option to upload & attach a video to a post into a custom field (video_high_resolution_mp4_video) (Actually created by MagicFields2, but this seems to be irrelevant).

I have a script that runs via cron to list all posts, finds all posts with the video_high_resolution_mp4_video custom field set and has an empty second custom field called video_vimeo_id.
The script will then upload video from video_high_resolution_mp4_video to Vimeo via the Vimeo API. This works and I get the Vimeo ID back as a string.

$video_id = $vimeo->upload($video_high_resolution_mp4_video);

I get this back

<blockquote>string(8) "34566961"</blockquote>

But, when I try to update the Vimeo ID into the post with update_post_meta, I get an error.

I'm doing

$foo = update_post_meta(get_the_id(), 'videos_vimeo_id', $video_id);

I am also doing

$foo2 = add_post_meta(get_the_id(), 'video_vimeo_id_callback', $video_id, TRUE);
and
$foo4 = add_post_meta(get_the_id(), 'videos_vimeo_id_upload_date', date(r), TRUE);

to prove its not a MagicFields issue, as video_vimeo_id_callback and videos_vimeo_id_upload_date are just new custom post field I made up.

$foo2 & $foo4 randomly returns false on some posts and not others.

I am testing with

if ($foo2 == FALSE) {
echo 'update_post_meta failed. Deleting video from Vimeo';
$delete_from_vimeo = $vimeo->call('vimeo.videos.delete',$video_id);
}
else {
echo 'update_post_meta succeeded';
}


I can't see any difference in the posts that work and the posts that fail.
Vimeo always returns a valid $video_id to work with.

So, out of a few hundred posts, I get an error on maybe 4 or 5 posts. I've deleted these posts and added them again. Each time update_post_meta and add_post_meta fails.

Watching the MySQL log (I've enabled query logging), it doesn't even do a MySQL query when update_post_meta or add_post_meta fails.
I can't see what WP logic decides not to even query the DB on these two functions.

Incidently, add_post_meta also fails at this point aswell

$foo2 = add_post_meta(get_the_id(), 'video_vimeo_id_callback', $video_id, TRUE);

Has anyone else had issues like this before? Or know whats going on?

Thanks


Update:

Just rewrote the code to test my sanity.

<?php
require_once '../wp-blog-header.php';
require_once 'vimeo.class.php';

$vimeo = new phpVimeo(VIMEO_CONSUMER_KEY, VIMEO_CONSUMER_SECRET, VIMEO_ACCESS_TOKEN, VIMEO_ACCESS_TOKEN_SECRET);
echo '<pre>';print_r($vimeo);echo '</pre>';

$args = array('order' => 'DESC', 'post_type' => array('video_library','post'), 'status' => array('pending','published'), 'posts_per_page' => -1);

if (isset($_GET['id'])) {
$args2 = array('p' => $_GET['id'], 'status' => array('draft','pending','published'));
$args = array_merge($args,$args2);

}

echo '<pre>';print_r($args);echo '</pre>';

$the_query = new WP_Query($args);

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

echo '<br /><strong> ('.$post->ID.') '.get_the_title(). '</strong>';

echo '<br />';

echo 'Post Type: '.get_post_type(get_the_id()).'<br />';

if (get_post_type(get_the_id()) == 'post') {
$video_high_resolution_mp4_video_id = get_post_meta(get_the_id(),'videos_high_resolution_mp4_video', TRUE);
$video_vimeo_id = get('videos_vimeo_id');

}
elseif (get_post_type(get_the_id()) == 'video_library') {
$video_high_resolution_mp4_video_id = get_post_meta(get_the_id(),'video_high_resolution_mp4_video', TRUE);
$video_vimeo_id = get('video_vimeo_id');
}


echo 'Attachment ID: '.$video_high_resolution_mp4_video_id.'<br />';
echo 'Vimeo ID: '.$video_vimeo_id.'<br />';

if (isset($video_high_resolution_mp4_video_id)) {
$video_high_resolution_mp4_video = wp_get_attachment_url($video_high_resolution_mp4_video_id);
echo 'Attachment URL: '.$video_high_resolution_mp4_video.'<br />';
}
else {
'can\'t get attachment id';
}

// we have no vimeo id
if (empty($video_vimeo_id) AND !empty($video_high_resolution_mp4_video)) {
echo 'No vimeo id for this, but a high res waiting to upload <br />';
echo '**** GOING TO UPLOAD **** <br />';

$video_high_resolution_mp4_video_local = preg_replace(WP_SITEURL_RX,WP_PATH ,$video_high_resolution_mp4_video);
echo 'Local Path: '. $video_high_resolution_mp4_video_local.'<br />';

// do the upload

$video_id = $vimeo->upload($video_high_resolution_mp4_video_local);

// catch errors

//
if (isset($video_id) AND !empty($video_id)) {
echo '** Upload to Vimeo OK! <br />';

// set the info on vimeo
// $vimeo->call('vimeo.videos.setTitle', array('title' => '('.get_the_id().') '.get_the_title(), 'video_id' => $video_id));
// $vimeo->call('vimeo.videos.embed.setPreset', array('preset_id' => VIMEO_PRESET_ID, 'video_id' => $video_id));

// set the wp meta info
$update_post_meta = update_post_meta($post->ID, 'videos_vimeo_id', date('r'), "");
$update_post_meta2 = update_post_meta($post->ID, 'videos_vimeo_id_callback', date('r'), "");
$update_post_meta3 = update_post_meta($post->ID, 'videos_vimeo_id_upload_date', date('r'), "");

if ($update_post_meta == FALSE) {
echo 'update_post_meta failed';
}
else {
echo 'update_post_meta ok';
}
echo ' | ';

if ($update_post_meta2 == FALSE) {
echo 'update_post_meta failed 2';
}
else {
echo 'update_post_meta ok 2';
}
echo ' | ';

if ($update_post_meta3 == FALSE) {
echo 'update_post_meta failed 3';
}
else {
echo 'update_post_meta ok 3';
}

}
else {
echo '** Upload to Vimeo FAILED! <br />';
}



}
else {
echo '<br />Already Vimto\'ed';
}


echo '<br /><a href="'.get_bloginfo('url').'/wp-admin/post.php?post='.get_the_id().'&action=edit">Edit post</a>';

echo '<hr />';

endwhile;

?>


$post-ID works and get_the_id() works fine.

I may have found the issue...

When I have this line

$video_id = $vimeo->upload($video_high_resolution_mp4_video_local);

The update_post_meta calls fail.

When I comment the above out and do

$video_id = 44444;

they work.

What makes no sense is that update_post_meta and $video_id are not currently linked together at all in this code.

Answers (6)

2012-01-05

John Cotton answers:

Shouldn't that be get_the_ID() ?

But even then, that only works in the loop (ie when the global $post is correctly set).

Without seeing your code I can't be sure, but I think your IDs might be wrong...

Are you able to output get_the_ID (get_the_id) and check whether the correct values are there?


meshint comments:

User defined functions are case-insensitive John.

I am in a loop...

$args = array('order' => 'DESC', 'post_type' => array('video_library','post'), 'status' => array('pending','published'), 'posts_per_page' => -1);

if (isset($_GET['id'])) {
$args2 = array('p' => $_GET['id']);
$args = array_merge($args,$args2);

}

print_r($args);

$the_query = new WP_Query($args);


And I can output get_the_id() and get the correct values.

// update_post_meta
echo '!!!!'.get_the_id().'!!!!'.$video_id.'!!!!';


Like I mentioned originally, its not 100% consistent.


John Cotton comments:

<blockquote>I am in a loop...</blockquote>

You may be in a loop, but are you in <em>the</em> loop?

It matters since the global $post may not be correctly set if you aren't and thus that function (inappropriately typed or not) won't work.

As suggested, you should try using $post->ID and see what that throws up.

It would be helpful to see more code...


meshint comments:

John, see my full code above.


John Cotton comments:

Do a print_r($video_id) after the vimeo call - it may be that you're not getting back what you think you're getting back...


meshint comments:

Thanks John.

My new code (above) doesn't even care about $video_id the update_post_meta calls fail.


This works

$update_post_meta = update_post_meta($post->ID, 'videos_vimeo_id', date('r'), "");
$update_post_meta2 = update_post_meta($post->ID, 'videos_vimeo_id_callback', date('r'), "");
$update_post_meta3 = update_post_meta($post->ID, 'videos_vimeo_id_upload_date', date('r'), "");

This fails

$video_id = $vimeo->upload($video_high_resolution_mp4_video_local);
$update_post_meta = update_post_meta($post->ID, 'videos_vimeo_id', date('r'), "");
$update_post_meta2 = update_post_meta($post->ID, 'videos_vimeo_id_callback', date('r'), "");
$update_post_meta3 = update_post_meta($post->ID, 'videos_vimeo_id_upload_date', date('r'), "");


This also fails

$video_id = $vimeo->upload($video_high_resolution_mp4_video_local);
unset($video_id);
$update_post_meta = update_post_meta($post->ID, 'videos_vimeo_id', date('r'), "");
$update_post_meta2 = update_post_meta($post->ID, 'videos_vimeo_id_callback', date('r'), "");
$update_post_meta3 = update_post_meta($post->ID, 'videos_vimeo_id_upload_date', date('r'), "");


John Cotton comments:

When you say "fails" do you mean the code runs (how do you know?) but nothing updates?

Or are you not certain that the code runs?

For instance is the code timing out? Have you tried putting the update meta calls before the vimeo upload?


meshint comments:

John,

I know the update_post_meta fail because I am testing with

if ($update_post_meta == FALSE) {
echo 'update_post_meta failed';
}
else {
echo 'update_post_meta ok';
}


I have tried putting them above, and they work. But, eventually I will be calling update_post_meta with data given back from the $vimeo->upload() call.

99% sure the $vimeo->upload() breaks update_post_meta which is very confusing.

I've already posted in the Vimeo API forum and they say its a WP issue.

Kinda lost right now.


John Cotton comments:

Well, let's think this through...

All update_post_meta does is make a database call to insert/update a row. There's no way that vimeo could be messing that up - it's completely independent and I'd eat my mouse mat if, somehow, the videmo code affected that.

However, the function and the call to it both rely on global values to do their work. Those <em>could be</em> affected.

$post seems the most likely (or, rather, the least unlikely) so how about storing the $post->ID value BEFORE the vimeo call and that using that local variable in the update meta calls?


John Cotton comments:

Are you able to post the code for the $vimeo->upload function?


meshint comments:

I'd eat my mouse mat too if that was the case.

Here I am setting $post_id_kb before the $vimeo->upload()
and it still fails

$post_id_kb= $post->ID;

// do the upload
$video_id = $vimeo->upload($video_high_resolution_mp4_video_local);
unset($video_id,$vimeo);


echo '** Upload to Vimeo OK! <br />';

$update_post_meta = update_post_meta($post_id_kb, 'videos_vimeo_id', date('r'));
$update_post_meta2 = update_post_meta($post_id_kb, 'videos_vimeo_id_callback', date('r'));
$update_post_meta3 = update_post_meta($post_id_kb, 'videos_vimeo_id_upload_date', date('r'));


meshint comments:

vimeo.class.php is here
https://github.com/vimeo/vimeo-php-lib/blob/master/vimeo.php


meshint comments:

http://media.toyota.co.uk/vimeo/send-to-vimeo-new.php?id=19057

This shows get_defined_vars() before and after the Vimeo upload


John Cotton comments:

<blockquote>vimeo.class.php is here
https://github.com/vimeo/vimeo-php-lib/blob/master/vimeo.php</blockquote>
Looks harmless enough.

You could try:
global $wpdb; echo $wpdb->last_error.' '; echo $wpdb->last_query;

after each update. At least you might see if there is a database problem...

Also, there are a number of hooks in update_metadata ('update_postmeta', 'updated_postmeta'). You could add some code to review the values in each of those and see if that sheds any light.


meshint comments:

Interesting John!

I get "MySQL server has gone away" right after $vimeo->upload

http://media.toyota.co.uk/vimeo/send-to-vimeo-new.php?id=19057

Obviously, it hasn't! Gotta be a variable conflict


John Cotton comments:

How big is http://media.toyota.co.uk/wp-content/uploads/2012/01/LEXUS-GS-450h-3.mov?

I'm assuming it's a few Mbs...more than 10?

If so, then that page returns feels to quick for something that's uploading a large file to another server (unless both machines have a very, very fast connection between them).

It might be worth putting some debug statements in the vimeo code to confirm what's actually going on. It could that there is some TCP lock out....




meshint comments:

Both have very very fast internet connections!

I'll add some debugging to Vimeo.


meshint comments:

Think I am getting somewhere.

Looks like the MySQL connection WordPress initiates at the top of the code is timing out during uploading of some videos.

As my original post mentions it was only some videos, so adding <em>$wpdb->last_error </em>suggested that larger videos cause MySQL time out.

WordPress doesn't re-initiate the connection with its <em>update_post_meta </em>(or probably any other function) which is why we get a <em>MySQL server has gone away</em> error.

Just going to do some testing with something like

$wpdb->query('SET session wait_timeout=6000');

2012-01-05

Julius J. answers:

Use function get_the_ID(); not get_the_id();

get_the_id(); dosn't exist :)


meshint comments:

From the PHP manual;

<blockquote>Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.</blockquote>

Try this code;

if (function_exists(get_the_ID)) {
echo 'get_the_ID exists<br />';
}

if (function_exists(get_the_id)) {
echo 'get_the_id exists';
}

2012-01-05

Sébastien | French WordpressDesigner answers:

get_the_ID
not
get_the_id

2012-01-05

Julio Potier answers:

if "<strong>get_the_ID</strong>" doen't work, try to add
global $post;
and replace
get_the_id();
by
$post->ID;


meshint comments:

It only doesn't work on certain posts! Tried both of those options.

2012-01-05

Christianto answers:

get_the_ID() only works if inside the loop..
where exactly your function run..?

you still have to supply the id if it run on cron outside any wordpress action/hook that query posts..


Christianto comments:

<blockquote>I have a script that runs via cron to list all posts, finds all posts with the video_high_resolution_mp4_video custom field set and has an empty second custom field called video_vimeo_id.</blockquote>

How you list all your posts? by using basic loop?
maybe you can use [[LINK href="http://codex.wordpress.org/Template_Tags/get_posts"]]get_posts[[/LINK]] to list all posts (its return all post object/data including post ID).

just in case that get_the_ID occasionally failed to return the post id.
or use what Julio said above.


meshint comments:

I'm in a loop. See my reply to John above

2012-01-06

Arnav Joy answers:

if get_the_ID(); or $post->ID does not works then add this

wp_reset_query();

before

$foo2 = add_post_meta(get_the_id(), 'video_vimeo_id_callback', $video_id, TRUE);


meshint comments:

Doesn't wp_reset_query destroy the loop ready for another loop?

That would kill the loop if I called it just before $foo2 ?