logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$12
What's wrong with my post_meta / update_post function?

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>

Matt Taylor | 07/22/11 at 9:11am Edit

Previous versions of this question: 07/22/11 at 10:10am

(3) Possible Answers Submitted...

Note: Matt Taylor felt their question was unanswered, so we granted them a refund.

Note: Matt Taylor requested a refund. They offered this explanation:

"Issue was not solved."

If no one challenges a refund request, then they are automatically granted and proccessed after 48 hours. Admins of this site only review refund requests if someone challenges the request. If you are curious about how we handled previous refund requests, you may read over all refund requests and their challenges.

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/22/11
    9:18am
    AdamGold says:

    Add:

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


    Below:
    global $post;


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

    Previous versions of this answer: 07/22/11 at 9:18am

    • 07/22/11 9:18am

      Matt Taylor says:

      returns

      string(6) "passes"

    • 07/22/11 9:18am

      AdamGold says:

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

    • 07/22/11 9:36am

      Matt Taylor says:

      int(476)

      476 is the correct post id number.

    • 07/22/11 10:44am

      AdamGold says:

      <?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.

    • 07/22/11 10:45am

      AdamGold says:

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

    • 07/22/11 11:20am

      Matt Taylor says:

      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.

  • avatar
    Last edited:
    07/22/11
    9:54am
    Dylan Kuhn says:

    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.

    • 07/22/11 9:55am

      Matt Taylor says:

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

    • 07/22/11 10:00am

      Dylan Kuhn says:

      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.

    • 07/22/11 10:10am

      Matt Taylor says:

      Thanks Dylan!

  • avatar
    Last edited:
    07/23/11
    3:27am
    Anna-marie Redpath says:

    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

    • 07/23/11 8:57pm

      Matt Taylor says:

      I'll check that. Thanks~

This question has expired.





Current status of this question: Refunded



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.