• Resolved jbucks

    (@jbucks)


    Hello, I’ve created a custom post type, called ‘land_listing’ along with a custom field called ‘cost’. From my home page, I’ve got some code to display the land listings. Everything seems to work, but a few minutes after I create a new land listing in the WP admin screen, or if I click into different sections of the WP admin screen, the custom field (‘cost’) disappears from both the home page and from the ‘cost’ meta box in the WP post edit screen. Here’s the custom post type creation code in functions.php:

    add_action('init', 'register_land_listing');
    
    function register_land_listing() {
    
    	$labels = array(
    		'name' => _x('Land Listings', 'post type general name'),
    		'singular_name' => _x('Land Listing', 'post type singular name'),
    		'add_new' => _x('Add New', 'Land Listing'),
    		'add_new_item' => __('Add New Land Listing'),
    		'edit_item' => __('Edit Land Listing'),
    		'new_item' => __('New Land Listing'),
    		'view_item' => __('View Land Listing'),
    		'search_items' => __('Search Land Listings'),
    		'not_found' =>  __('Nothing found'),
    		'not_found_in_trash' => __('Nothing found in Trash'),
    		'parent_item_colon' => ''
    	);
    
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		// 'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => true,
    		'menu_position' => null,
    		'supports' => array('title','editor','thumbnail')
    	  ); 
    
    	register_post_type( 'land_listing' , $args );
    }
    
    add_action("admin_init", "admin_init");
    
    function admin_init(){
    	  add_meta_box("cost-meta", "Land cost", "land_cost", "land_listing", "normal", "low");
    }
    
    function land_cost(){
    	  global $post;
    	  $custom = get_post_custom($post->ID);
    	  $land_cost = $custom["land_cost"][0];
    
    	  echo '<label>For rent or free:</label>
    	  <input name="land_cost" value="' . $land_cost . '" />';
    
    }
    
    add_action('save_post', 'save_details');
    
    function save_details() {
      global $post;
    
      update_post_meta($post->ID, "land_cost", $_POST["land_cost"]);
    
    }

    And here’s the code in my index.php file to display a list of the land listings:

    query_posts(
    				   array(
    					  'post_type' => array(
    						 'land_listing'
    					  ),
    					  'posts_per_page' => 10,
    					  'post_status' => 'publish',
    				   )
    				);
    
    				while (have_posts()) : the_post(); 
    
    				  the_title('<h3>', '</h3>');
    				  the_content('Read the full post ?');
    				  echo '<p>' . get_post_meta($post->ID, 'land_cost', true) . '</p>';
    				endwhile;

    Thanks in advance for any help, I’ve spent hours on this without getting anywhere!!!

    PS – I’m not sure where custom fields for custom post types are stored in the database? I tried to look into the DB with phpmyadmin to see if the data was saving probably, but no joy…

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter jbucks

    (@jbucks)

    Problem solved, but I don’t understand why. This function (from the code above)…

    function land_cost(){
    	  global $post;
    	  $custom = get_post_custom($post->ID);
    	  $land_cost = $custom["land_cost"][0];
    
    	  echo '<label>For rent or free:</label>
    	  <input name="land_cost" value="' . $land_cost . '" />';
    
    }

    …needed to be replaced by the following code:

    function land_cost(){
      global $post;
      $custom = get_post_custom($post->ID);
      $land_cost = $custom["land_cost"][0];
      ?>
      <label>Cost:</label>
      <input name="land_cost" value="<?php echo $land_cost; ?>" />
      <?php
    }

    It would be great if someone could explain to me why this worked…

    Thread Starter jbucks

    (@jbucks)

    Nevermind, it didn’t work. The problem is occurring again – I waited a few minutes, and suddenly the ‘cost’ field disappears from the most recent land listing entry on the home page, along with the ‘cost’ value in the meta box (in WP admin) for that post…

    this is really frustrating…

    Thread Starter jbucks

    (@jbucks)

    OK, think I’ve got it. I found this link which describes exactly the problem I was having:
    https://stackoverflow.com/questions/2539951/wordpress-add-meta-box-weirdness

    So I changed the save_details() function from my code above by checking if wordpress is auto-saving, and so far it’s working:

    function save_details() {
      global $post;
    
      $post_id = $post->ID;
    
      // to prevent metadata or custom fields from disappearing...
      if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return $post_id;
    
      update_post_meta($post_id, "land_cost", $_POST["land_cost"]);
    
    }

    I’m so happy that you give the fix!
    I understood nothing about this problem.

    Thanks a lot.

    Thanks for the update jbucks, saved my life!

    Works perfect. Thanks for posting the fix!!!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom fields data disappears after a few mins?’ is closed to new replies.