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

What's wrong with my post_meta / update_post function? WordPress

  • REFUNDED

Somethings wrong with the function below. Post meta is not being updated and the post content does not show the changes when the page is first reloaded. The wining answer will address both of these complaints.

At top of page template:
if ( isset( $_POST['drw_inventory'] ) && wp_verify_nonce($_POST['drw_inventory'],'update_driftwood_inventory') )
{ //if nonce check succeeds.
global $post;
$postid = $post->ID;

$price1_data = $_POST['priceone'];
$price1_data = str_replace(',', '', $price1_data);
update_post_meta($postid,'_dappcf_i_priceone',$price1_data);

$price2_data = $_POST['pricetwo'];
$price1_data = str_replace(',', '', $price2_data);
update_post_meta($postid,'_dappcf_i_pricetwo',$price2_data);

$milage_data = $_POST['milage'];
$milage_data = str_replace(',', '', $milage_data);
update_post_meta($postid,'_dappcf_i_mileage',$milage_data);

$stocknum_data = $_POST['stocknum'];
update_post_meta($postid,'_dappcf_i_stocknum',$stocknum_data);

$my_post = array();
$my_post['ID'] = $postid;
$my_post['post_content'] = $_POST['description']; wp_update_post( $my_post );
}


In page:

<?php
$priceone = get_post_meta($post->ID, '_dappcf_i_priceone', true);
$pricetwo = get_post_meta($post->ID, '_dappcf_i_pricetwo', true);
$milage = get_post_meta($post->ID, '_dappcf_i_mileage', true);
$thecontent = get_the_content();
?>


<form method="post" action="">
<?php wp_nonce_field('update_driftwood_inventory','drw_inventory'); ?>
<button class="buttonPro drbtn-ltblue " type='submit'>Save Changes</button>

<input type='text' name='priceone' value='<?php if($priceone) echo number_format($priceone) ?>' class="data dataedit yellow" />

<input type='text' name='pricetwo' value='<?php if($pricetwo) echo number_format($pricetwo) ?>' class="data dataedit yellow" />

<input type='text' name='milage' value='<?php if($milage) echo number_format($milage) ?>' class="data dataedit dot" />

<textarea name="description" cols="140" rows="6"><?php echo $thecontent ?></textarea>

Answers (3)

2011-07-22

AdamGold answers:

Add:
die(var_dump($post->ID));

Below:
global $post;

There should be a blank page with a number. Please paste here this number.


Matt Taylor comments:

returns string(6) "passes"


AdamGold comments:

Okay I've edited my answer. Please take a look.


Matt Taylor comments:

int(476)
476 is the correct post id number.


AdamGold comments:

<?php

$priceone = get_post_meta($post->ID, '_dappcf_i_priceone', true);

$pricetwo = get_post_meta($post->ID, '_dappcf_i_pricetwo', true);

$milage = get_post_meta($post->ID, '_dappcf_i_mileage', true);

$thecontent = get_the_content();

?>

Where do you put the above code? Please try to add die(var_dump($post->ID));
as well to see if you're getting the metas from the right post.


AdamGold comments:

By the way, you only show part of the form. Can I see the whole code of it?


Matt Taylor comments:

The post meta values are correct. If they are set within wp-admin, the values show on the page correctly. I sent you a pm.

2011-07-22

Dylan Kuhn answers:

As template code, this won't run until after the post and its metadata have already been queried and cached. I don't know why the postmeta isn't updated, but if you want to see updated values in your template, I would set those variables when the updates are made. Then in the template, set them to the cached values only if they're not already set.


Matt Taylor comments:

Is there an answer I'm missing in there? :)


Dylan Kuhn comments:

Treat it as a free hint for AdamGold - hopefully he'll get you some working code. I think the template code where you set $priceone, $pricetwo, $mileage, and $thecontent is important - add that to the question.


Matt Taylor comments:

Thanks Dylan!

2011-07-23

Anna-marie Redpath answers:

Hi I am on the mobile so apologies may have missed something in the question but since it sounded like a problem I had might be related. Did you ever have any uppercase in your meta keys ?

Weirdly depending on your db collation get post meta Is case sensitive but update post meta is not


Is when u do an update it might update the 'old' uppercase even though your key is lowercase now .....but then notbe able to retrieve it. I posted a trac and a blog post about it - see anmari.com - last night.


Get into phpmyadmin to see exactly what is in the db


Matt Taylor comments:

I'll check that. Thanks~