0
    <?php 
foreach ( $posts_array as $post ) {

    $post_id = wp_insert_post($post,true);
    //var_dump($post);

    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET VenueLongitude = '$VenueLongitude' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET VenueLatitude = '$VenueLatitude'  WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET  Tag = '$Tag' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET IsDeleted  = '$IsDeleted' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET AdminID = '$AdminID' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET CreatedAt = '$CreatedAt' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET VenueStreet = '$VenueStreet' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET VenueNumber = '$VenueNumber' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET VenueDistrict = '$VenueDistrict' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET VenueCity = '$VenueCity'  WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET VenueWard = '$VenueWard' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET PriceAverage = '$PriceAverage' WHERE ID=$post_id"));
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET Near = '$Near' WHERE ID=$post_id"));

    }
    ?> 

I attached the code in which I run mutli wpdb->query in a foreach loop. But after the loop run 1 time it stopped immediately, it just runs only 1 time. It is ok if I just run 1 wpdb->query in the loop. How can I fix this problem? Anybody can help me?

Thanks a lot.

  • Your code has serious security issues - check the reference for the proper way of using wpdb::prepare(). Secondly, you could use wpdb::update() instead of $wpdb->query(). And third, none of the fields you're updating (e.g. VenueLongitude) exists in the standard WordPress posts table. – Sally CJ Nov 15 '18 at 17:26
  • If you want to update custom fields of a post, you can use update_post_meta(). – Sally CJ Nov 15 '18 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.